Free Code Snippets in C#, Net Framework, Office 365, ASP.Net,WPF, Visual Studio, SQL Server, Antivirus free
#

Asp MVC: Output Data from SQL Server directly to a List <Type>

 

Normal call: Get data with Linq-EF from a database table

Disadvantage: the results are of type var and have yet to be manually transferred to a list <data model>

var dsFollower = await _dbContext.tbl_User_Followers.Select(u => u.IDUser == IDOwner).ToListAsync();

 

Load Sql data in list:

This data query directs the results of a data table directly into a list of the same data model type.

To do this, the table [tbl_User_Followers] of the list content type must be <User_FollowerModel>.

//< get Follower >

List<User_FollowerModel> list_Follower =

await _dbContext.tbl_User_Followers.Where(u => u.IDUser == IDOwner).ToListAsync();

//</ get Follower >

 

 

 

Output in the view

So you can output the data in the View view

Asp Core MVC

You go through with foreach (..) the list and outputs the results with Html.Raw (text)

<p>

    Follower: @Model.sumFollowers

    @{

        foreach (var follower in Model.List_Followers)

        {

          <a href="~/User/User_Page/@Html.Raw(follower.IDFollower)">

          <img src="~/User_Files/User_Images/40/@Html.Raw("User_Image_" + follower.IDFollower + ".jpg")" /></a>

        }

    }

</p>

 

 

View example

Output of the file Readdy / Views / User / User_Page.cshtml

@model Readdy.Models.User_Page_ViewDataModel

 

@{

    ViewData["Title"] = "😀 User Page/" + Model.Ownername;

    Layout = "~/Views/Shared/_Layout.cshtml";

}

 

<style>

    .divMain {

        -webkit-box-orient: vertical;

        -webkit-box-direction: normal;

        flex-direction: column;

        vertical-align: top;

        display: inline-block;

        width: 80%;

        /*max-width: 80%;*/

        overflow-wrap: break-word;

        overflow: auto;

        margin: 0px;

        margin-top: 2px;

        padding: 10px;

        background-color: rgba(250, 250, 250, 0.9);

        border-radius: 8px 8px 8px 8px;

    }

 

        /* resize images */

        .divMain img {

            max-width: 100%;

            height: auto;

        }

 

        .divMain td {

            width: auto;

            max-width: 700px;

        }

 

 

 

    .divRight {

        display: inline-block;

        /*display: block;*/

        /*float: right;*/

        margin: 0;

        padding: 0px;

        background-color: rgba(235, 235, 235, 0.74);

    }

 

    .divList {

        width: 100%;

    }

 

    .divItem {

        width: 100%;

        display: block;

        padding: 4px;

        margin-top: 10px;

        box-shadow: 0 1px 2px 0 rgba(0,255,0,0.2), 0 0 0 1px rgba(0,0,0,0.1);

        transition: box-shadow 1s ease-in-out;

        border-radius: 2px;

        outline: none;

        background-color: rgba(255,255,255,1)

    }

 

        .divItem:hover {

            background-color: rgba(182, 255, 0,0.5);

        }

 

    .divOwner {

        width: 100%;

        display: block;

        padding: 4px;

        margin-top: 10px;

        outline: none;

        font-size: x-small;

    }

 

        .divOwner:hover {

            background-color: rgba(182, 255, 0,0.5);

        }

</style>

 

<h3 style="margin:6px;font-size:large">

    <img src="~/images/logo/logo_small.png" style="height:18px;" />

    ► <img src="~/images/Ico/icoProfil.png" style="height:20px;vertical-align:text-bottom;">

    : <a href="/user/User_Page/@Model.IDOwner" style="font-weight:bold ">@Model.Ownername </a>

</h3>

@*--------< divMain >--------*@

<div id="divMain" class="divMain">

    @*------< Image >--------*@

    <img src="@Html.Raw("/User_Files/User_Images/400/User_Image_" + Model.IDOwner + ".jpg")" style="border-radius:50%;display:inline-block;float:left;">

    @*------</ Image >--------*@

 

 

    @*------< Description >--------*@

    <div id="divInfos">

        <p>Started: @Model.dtCreate</p>

        <p>

            Follower: @Model.sumFollowers

            @{

                foreach (var follower in Model.List_Followers)

                {

                    <a href="~/User/User_Page/@Html.Raw(follower.IDFollower)"> <img src="~/User_Files/User_Images/40/@Html.Raw("User_Image_" + follower.IDFollower + ".jpg")" /></a>

                }

            }

        </p>

 

 

 

 

        <p>

            Follows: @Model.sumFollows

            @{

                foreach (var follows in Model.List_Follows)

                {

                    <a href="~/User/User_Page/@Html.Raw(follows.IDUser)"> <img src="~/User_Files/User_Images/40/@Html.Raw("User_Image_" + follows.IDUser + ".jpg")" /></a>

                }

            }

        </p>

        <p>Views: @Model.sumViews</p>

        <span>

            Info:

            @Model.Description

        </span>

    </div>

    @*------</ Description >--------*@

 

    @*------< Notes >--------*@

    <Div class="divList" style="width:40%">

 

        ►<a href="~/notes/User_Notes/@Model.IDOwner">📃Notes</a>

 

 

        @*------< Loop: Details >------*@

        @{

            int iLoop = 0;

            foreach (var note in Model.List_Notes)

            {

                iLoop++;

                @*----< row >----*@

                <a href="~/Notes/Details/@note.IDNote" title="@Html.DisplayFor(modelItem => note.DtEdit): @Html.DisplayFor(modelItem => note.Title)" style="text-decoration:none;display:block;">

                    <Div class="divItem" style="display:inline-block;margin:2px;">

                        @{

                            if (iLoop < 3)

                            {

                                if (note.sumImages > 0)

                                {

                                    <div style="float:left; margin:2px;margin-right:10px;">

                                        <img src="@("/User_Files/Notes/Images/Image_" + note.IDNote + "_0.jpg" )" style="width:140px;" />

                                    </div>

                                }

                            }

 

                        }

 

                        <span style="text-align:left;font-size:large;padding:2px;">

                            @Html.DisplayFor(modelItem => note.Title)

                        </span>

 

                        <div style="float:right;text-align:left;font-size: 12px; color: #757575;margin-top:10px;">

                            <span style="text-align:left;font-size: 12px; color: #757575;">

 

                                @Html.DisplayFor(modelItem => note.DtEdit)

 

                                (👁@Html.DisplayFor(modelItem => note.sumViews))

                            </span>

                        </div>

                    </Div>

                </a>

                @*----</ row >----*@

 

            }

        }

 

    </Div>

    @*----</ Loop: Details >------*@

    @*------</ Notes >--------*@

</div>

@*--------</ divMain >--------*@

 

 

Complete asp.Net Core MVC example

 

Shows: how to directly retrieve a table from the SQL Server database with Linq and EF Entity Framework in a list <data model> list

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using Microsoft.AspNetCore.Mvc;

using Microsoft.EntityFrameworkCore;

using Readdy.Data;

using Readdy.Models;

using Microsoft.AspNetCore.Authorization;

using Microsoft.AspNetCore.Hosting;

using System.IO;

using Microsoft.AspNetCore.Http;

 

namespace Readdy.Controllers

{

    public class UserController : Controller

    {

 

        #region Controller Init

        private readonly ApplicationDbContext _dbContext;

        private readonly IHostingEnvironment _hostingEnvironment;

        public UserController(ApplicationDbContext dbContext, IHostingEnvironment hostingEnvironment)

        {

            //----< Init: Controller >----

            _dbContext = dbContext;

            _hostingEnvironment = hostingEnvironment;

            //----</ Init: Controller >----

        }

        #endregion

 

        public async Task<IActionResult> User_Page(long Id)

        {

            //-------------< User_Page_load() >-------------

            //< init >

            long IDOwner = Id;

            //</ init >

 

            //--< Get User ID >--

            //internal referenz-Number for tracking in tables

            long IDCurrent_User = await UserInfo_Methods.getIDUser_as_Number(this.User, _dbContext);

            //--</ Get User ID >--

 

            //< content_logger >

            await Counter_Logger.counter_Note_List_erhoehen(IDCurrent_User);

            //</ content_logger >

            //--< Get Linq.Query >--

 

            //*gets last 10 Notes with View_Sum

            var query = (from n in _dbContext.tbl_Notes

                         where n.IDUser == Id

                         orderby n.IDNote descending

                         select n).Take(20);

 

            //--</ Get Linq.Query >--

 

            //< get Owner >

            var owner = await _dbContext.Users.SingleOrDefaultAsync(u => u.IDUser == IDOwner);

            if (owner == null)

            {

                return Content("Owner not found");// NotFound();

            }

            //</ get Owner >

 

            //< get Sums >

            var userSums = await _dbContext.tbl_User_Sums.SingleOrDefaultAsync(u => u.IDUser == IDOwner);

            //</ get Sums >

 

            //< get Follower >

            //var dsFollower = await _dbContext.tbl_User_Followers.Select(u => u.IDUser == IDOwner).ToListAsync();

            List<User_FollowerModel> list_Follower = await _dbContext.tbl_User_Followers.Where(u => u.IDUser == IDOwner).ToListAsync();

            //</ get Follower >

            //< get Follows >

            List<User_FollowerModel> list_Follows = await _dbContext.tbl_User_Followers.Where(u => u.IDFollower == IDOwner).ToListAsync();

            //</ get Follows >

 

            //--< fill Notes >--

            List<NoteModel> list_Notes = new List<NoteModel>();

            foreach (var row in query)

            {

                //--< Row to Data >--

                //< correct >

                string sShort = row.Text;

                if (sShort.Length > 255) { sShort = sShort.Substring(0, 255); }

 

                row.Text = sShort;

                //</ correct >

 

                //< Data >

                NoteModel note = new NoteModel();

                note = row;

                //</ Data >

 

                //< add >

                list_Notes.Add(note);

                //</ add >

                //--</ Row to Data >--

            }

            //--</ fill Notes >--

 

            User_Page_ViewDataModel data = new User_Page_ViewDataModel();

            data.List_Notes = list_Notes;

            data.IDOwner = IDOwner;

            data.Ownername = owner.UserName;

            data.Description = owner.User_Description;

            data.dtCreate = Convert_Methods.DateNull_to_Date(owner.dtCreated);

            data.sumFollowers = Convert_Methods.IntNull_to_Int(owner.sumFollowers);

            if (userSums == null)

            {

                data.sumViews = 0;

            }

            else

            {

                data.sumViews = Convert_Methods.IntNull_to_Int(userSums.intSumViews_Others); //User_SumModel

            }

            data.List_Followers = list_Follower ;

            data.List_Follows = list_Follows;

            //< out to view >

            //return View(data_to_View);

            return View("User_Page", data);

            //</ out to view >

            //-------------</ User_Page_load() >-------------

        }

    }

}

 

Mobile
»
EF: Determine new ID of a data record
»
Asp MVC: Output Data from SQL Server directly to a List Type
»
Linq: Query data with a left join
»
Asp Linq: Collect data with a LINQ query from SQL Server

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de