我想在一个视图中使用两个模型,但是从我理解我的程序只是看不到模型内的任何对象。如何在一个视图中使用两个IENUMrable模型
这是我的代码。
型号:
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; }
}
指数控制器代码:
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);
}
查看代码:
@model MvcMusicStore.Models.ModelMix
<h2>Index</h2>
@Html.DisplayNameFor(model => model.PostsObject.PostContent)
当我尝试执行我的计划,我得到这个错误:
CS1061: The " System.Collections.Generic.IEnumerable 'does not contain a definition of" PostContent "not found method of expanding" PostContent ", which takes a first argument of type' System.Collections.Generic.IEnumerable "
我如何使它按预期工作?在互联网上有很多像我这样的问题,但我找不到与我的情况相符的问题。
需要迭代它们在\的foreach –