【发布时间】:2014-05-29 11:42:54
【问题描述】:
在主页(HomeController,视图索引)上显示来自其他模型(新闻)的最后一条新闻的最佳方式是什么?
我在共享视图目录中创建了部分视图,它看起来是这样的:
@model IEnumerable<MyApp.Models.News>
@foreach (var item in Model.Where(x => x.IsActive ==
true).OrderByDescending(x => x.DateCreated).Take(2)) {
<li>
@Html.ActionLink(item.DateCreated.ToString(), "Details", "News", new { id = item.NewsId})
<p>@item.Content</p>
</li>
}
但是这样我会出错:
An exception of type 'System.ArgumentNullException' occurred in
System.Core.dll but was not handled in user code
模型不为空,我的数据库中有两个“活动”记录,该模型的索引文件也是正确的,但我不知道如何将这个部分呈现到我的索引主页...(现在我在 HomeController 的 Index.cshtml 视图中 @Html.Partial("_LatestNews"))
感谢您提前提供任何帮助。
【问题讨论】:
-
我需要一些操作来呈现这样的局部视图吗?
-
你打电话的地方,型号类型必须像 MyApp.Models.News chk
标签: asp.net-mvc model partial-views