简体   繁体   中英

ASP.Net MVC Pass EF Data to a Model

I am new to MVC and Entity Framework, and would like some assistance in passing data to a model so that I can make my view a strongly typed view and get the information out as I need.

In the about action in the home controller i want to pull out data from my SiteContent table that is for the About Page

        var sc = db.siteContents.Where( x => x.Page == "About").ToList();
        return View(sc);

A couple of questions I have around this, is first, how do I get this into my Model SiteContent

    public class SiteContent
{
    public int ContentId { get; set; }
    public string ContentType { get; set; }
    public string Page { get; set; }
    public string ContentHeader { get; set; }
    public string Content { get; set; }
    public DateTime OriginalPostDate { get; set; }
    public DateTime UpdatePostDate { get; set; }
    public string ImageFileName { get; set; }
    public bool ContentReleased { get; set; }
}

I am uncertain on how I should proceed from here.

The from here I would like to use a strongly typed view to get the data by using my model.

Any and all help very much appreciated.

Thanks

Simon

Define your view like this:

@model IEnumerable<SiteContent>
<table>
   <tr>
      <td>ContentType</td>
      // other data
   </tr>
   @foreach(var item in Model)
   {
        <tr>
            <td>@item.ContentType</td>
            // other data
        <tr>
   }
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM