我正在尝试在部分视图中选择并显示每个新闻类别的最近 5 次提交。新闻表具有类别 ID 作为外键。我在下面有这段代码,但它在测试中没有显示任何内容。任何人都可以提供帮助。
新闻控制器中的代码
public ActionResult _HomeCatNews()
{
var info = db.News
.GroupBy(r => r.CatId)
.SelectMany(g => g.OrderBy(r => r.RevDate)
.Take(5))
.ToList();
return PartialView();
}
局部视图代码如下
@model IEnumerable<KPortal.Models.HomeNewzCat>
@using KPortal.Helpers
@foreach (var item in Model) {
<ul>
<li>
<div class="image">
<a href="#">@Html.DisplayFor(modelItem => item.NewsPic)</a>
</div>
<div class="details">
<h5><a href="#">@Html.Raw(Html.Truncate(item.NewsContent, 55, ""))</a></h5>
<span class="date">
@Html.DisplayFor(modelItem => item.RevisionDate)
@Html.DisplayFor(modelItem => item.NewSource)
</span>
</div>
@Html.DisplayFor(modelItem => item.NewsTitle)
</li>
</ul>
}
家NewzCat模型
namespace KPortal.Models
{
public class HomeNewzCat
{
[Display(Name = "Category ID")]
public int CategoryID { get; set; }
[Display(Name = "Category")]
public string Category { get; set; }
[Display(Name = "Revision Date")]
public DateTime RevisionDate { get; set; }
[Display(Name = "News Title")]
public string NewsTitle { get; set; }
[Display(Name = "Story Picture")]
public string NewsPic { get; set; }
[AllowHtml]
[Display(Name = "News Content")]
public string NewsContent { get; set; }
[Display(Name = "Source")]
public string NewSource { get; set; }
public DateTime Date { get; set; }
}
}