freiberuflicher Software Entwickler C#, vb.Net, vba, UWP, WPF, WinForms, SQL Server, Access, Excel, ASP.Net Core MVC Telefon: 07022/9319004   Kontakt
#

Asp MVC: Daten vom SQL Server direkt in eine List<Typ> ausgeben

 

Normaler Aufruf: Hole Daten mit Linq-EF aus einer Datenbank-Tabelle

Nachteil: die Ergebnisse sind vom Typ var  und müssen erst noch manuell in eine List<Datenmodel> überführt werden

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

 

Sql-Daten in Liste laden:

Mit dieser Datenabfrage leiten sie die Ergebnisse einer Datentabelle direkt in eine Liste vom gleichen Datenmodel-Typ.

Hierzu muss die Tabelle [tbl_User_Followers] vom Listen-Inhaltstyp: <User_FollowerModel> sein.

//< get Follower >

List<User_FollowerModel> list_Follower =

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

//</ get Follower >

 

 

 

Ausgabe in der View

So kann man die Daten in der Anzeige View ausgeben

Asp Core MVC

Man durchläuft mit foreach(..) die Liste und gibt die Ergebnisse mit Html.Raw(text) aus

<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 Beispiel

Ausgabe der Datei 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 >--------*@

 

 

Komplettes asp.Net Core MVC Beispiel

 

Zeigt: wie ruft man direct eine mit Linq und EF Entity Framework eine Tabelle von der SQL Server Datenbank ab in eine List<Datenmodel> Liste ab

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: Neue ID eines Datensatzes ermitteln
»
Asp MVC: Daten vom SQL Server direkt in eine List ausgeben
»
Linq: Daten mit einer Left Join abfragen
»
Asp Linq: Daten mit einer LINQ Datenabfrage vom SQL Server abholen

.

Jobs, Projekte, Angebote für Freiberufler, Selbstständig an Raimund.Popp@Microsoft-Programmierer.de