0

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

7
  • Are you wanting a collection of SiteContent (which is what you currently have) of a single SiteContent object? Commented Aug 20, 2015 at 21:51
  • Is SiteContent the model used in your DbContext or is this a POCO? Also, you're pulling a list (.ToList); did you mean to grab SingleOrDefault()? Commented Aug 20, 2015 at 21:51
  • So you can easily do that by using IEnumerable<SiteContent> as a model for your view. Commented Aug 20, 2015 at 21:53
  • @BradChristie yes, siteContext is used in my DbContext ` public virtual DbSet<siteContent> siteContents { get; set; }` Commented Aug 20, 2015 at 22:13
  • @SirwanAfifi i thought i would want an IEnumerable, but dont know how I should implement it Commented Aug 20, 2015 at 22:14

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

5 Comments

using this example i get this error back "The model item passed into the dictionary is of type 'System.Collections.Generic.List1[InspireFitnessForAll.WebSite.DataModel.siteContent]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[InspireFitnessForAll.WebSite.Models.SiteContent]'."
Check your namespace for SiteContent, It seems that your are accessing SiteContent in your DataModel folder not Models
ive just renamed the model to PageContent and have the same issue.
think i may have just spotted my mistake
Thank you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.