How to use two IENumerable models in one view

I am trying to use two models in one view, but from what I understand, my program simply does not see any objects in the models.

Here is my code.

Models:

public class Album { [Key] public int ThreadId { get; set; } public int GenreId { get; set; } public string Title { get; set; } public string ThreadByUser { get; set; } public string ThreadCreationDate { get; set; } public string ThreadContent { get; set; } public Genre Genre { get; set; } public List<Posts> Posty { get; set; } } public class Posts { [Key] public int PostId { get; set; } public int ThreadId { get; set; } public string PostTitle { get; set; } public string PostContent { get; set; } public string PostDate { get; set; } public string PosterName { get; set; } public Album Album { get; set; } } public class ModelMix { public IEnumerable<Posts> PostsObject { get; set; } public IEnumerable<Album> ThreadsObject { get; set; } } 

Index Controller Code:

  public ActionResult Index(int id) { ViewBag.ThreadId = id; var posts = db.Posts.Include(p => p.Album).ToList(); var albums = db.Albums.Include(a => a.Genre).ToList(); var mixmodel = new ModelMix { PostsObject = posts, ThreadsObject = albums }; return View(mixmodel); } 

View code:

 @model MvcMusicStore.Models.ModelMix <h2>Index</h2> @Html.DisplayNameFor(model => model.PostsObject.PostContent) 

And when I try to execute my program, I get this error:

CS1061: System.Collections.Generic.IEnumerable 'does not contain a definition of' PostContent 'no extension method found' PostContent 'that takes the first argument of type' System.Collections.Generic.IEnumerable '

How can I make it work as intended? There are many questions on the Internet like mine, but I could not find a suitable case for me.

+6
source share
3 answers

Quoting by models can be a bit confusing to start with in MVC just because template helpers ( Html.DisplayFor and Html.EditorFor ) can be provided by templates that the assistant will automatically call for each item in the collection. This means that if you are new to MVC and you do not understand that a DisplayTemplate or EditorTemplate has not already been provided for the collection, it looks as simple as that:

 @Html.DisplayFor(m => m.SomePropertyThatHoldsACollection) 

- that’s all you need. Therefore, if you have already seen something like this, it is possible, therefore, you made the assumption that this will work. However, suppose for a moment that a template has not been provided. You have two options.

First, the easiest way would be to use foreach over the collection:

 @foreach (var post in Model.PostsObject) { @Html.DisplayFor(m => post.PostTitle) // display other properties } 

You can also use a for loop, but with IEnumerable<T> there is no indexer, so this will not work:

 @for (int i = 0; i < Model.PostsObject.Count(); i++) { // This generates a compile-time error because // the index post[i] does not exist. // This syntax would work for a List<T> though. @Html.DisplayFor(m => post[i].PostTitle) // display other properties } 

If you still want to use the for loop, you can use it like this:

 @for (int i = 0; i < Model.PostsObject.Count(); i++) { // This works correctly @Html.DisplayFor(m => post.ElementAt(i).PostTitle) // display other properties } 

So use what you prefer. However, at some point it would be nice to learn patterns for your types . (Note: Although this article was written for MVC 2, the tip still applies.) They allow you to remove the loop logic from your views, while keeping them cleaner. In combination with Html.DisplayFor or Html.EditorFor they will also generate the correct element name to bind to the model (which is great). They also allow you to reuse a presentation for a type.

One final comment I would make is that the names of your properties are a bit detailed:

 public class ModelMix { public IEnumerable<Posts> PostsObject { get; set; } public IEnumerable<Album> ThreadsObject { get; set; } } 

We already know that these are objects, so there is no need to add them to the end. This is more readable:

 public class ModelMix { public IEnumerable<Posts> Posts { get; set; } public IEnumerable<Album> Threads { get; set; } } 
+6
source

You need to repeat them like this:

 @model MvcMusicStore.Models.ModelMix <h2>Index</h2> @for(var i=0; i<model.PostsObject.Count(); i++) { @Html.DisplayNameFor(model => model.PostsObject[i].PostContent) } 

And it's also better to keep IList instead of IEnumerable, since it will have the Count property, instead of using the Count () method

0
source

Usually you need to iterate over each element if you pass in any type of IEnumerable<> . Since you created the semi-complex model, you need to display the foreach element in each list. Here is an example based on ASP.NET MVC tutorials that I think will help you a bit:

 @model IEnumerable<ContosoUniversity.Models.Course> @{ ViewBag.Title = "Courses"; } <h2>Courses</h2> <p> @Html.ActionLink("Create New", "Create") </p> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.CourseID) </th> <th> @Html.DisplayNameFor(model => model.Title) </th> <th> @Html.DisplayNameFor(model => model.Credits) </th> <th> Department </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.CourseID) </td> <td> @Html.DisplayFor(modelItem => item.Title) </td> <td> @Html.DisplayFor(modelItem => item.Credits) </td> <td> @Html.DisplayFor(modelItem => item.Department.Name) </td> <td> @Html.ActionLink("Edit", "Edit", new { id=item.CourseID }) | @Html.ActionLink("Details", "Details", new { id=item.CourseID }) | @Html.ActionLink("Delete", "Delete", new { id=item.CourseID }) </td> </tr> } </table> 

Typically, most users use ICollection for their lists, where an example can be in an object:

 public virtual ICollection<Post> Posts { get; set; } 

Source: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net- mvc-application

I would advise starting from the very beginning, as this will help you understand why you need it: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc

-1
source

All Articles