【发布时间】:2014-01-06 15:58:21
【问题描述】:
我试图在一个视图中使用两个模型,但据我了解,我的程序在模型中看不到任何对象。
这是我的代码。
型号:
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:“ System.Collections.Generic.IEnumerable '不包含定义 “PostContent”的未找到扩展“PostContent”的方法,其中 接受类型为“System.Collections.Generic.IEnumerable”的第一个参数 "
我怎样才能让它按预期工作?网上有很多类似我的问题,但我找不到任何符合我的情况。
【问题讨论】:
-
你需要在 for\foreach 中迭代它们
标签: asp.net-mvc asp.net-mvc-4 model ienumerable