【发布时间】:2015-05-25 09:03:27
【问题描述】:
我想在我的主页上显示:所有版块(所有链接类别和该版块中的一条最新新闻记录)。 请帮我完成我的代码。 非常感谢。
我的 DbContext 类:
public partial class xxDbContext : DbContext
{
public xxDbContext()
: base("name=xxDbConnection") { }
public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<Section> Sections { get; set; }
public virtual DbSet<News> News { get; set; }
}
public partial class Section
{
public int Id { get; set; }
public string Name { get; set; }
public virtual List<Category> Categories { get; set; }
}
public partial class Category
{
public int Id { get; set; }
public int SectionId { get; set; }
public string Name { get; set; }
public virtual Section Section { get; set; }
}
public partial class News
{
public int Id { get; set; }
public int CateId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
}
我的控制器
public ActionResult NewsInSec()
{
var model = db.Sections.Where(m => m.Publish).ToList();
return PartialView("NewsInSec", model);
}
我的看法
@model IEnumerable<xx.Models.Section>
<div>
@foreach (var sect in Model)
{
<ol class="breadcrumb">
<li><a href="/Section/@sect.Id">@sect.Name</a></li>
@foreach (var cate in sect.Categories)
{
<li><a href="/Cate/@cate.Id">@cate.Name</a></li>
}
</ol>
**foreach (var item in sect.Categories.SelectMany(c => c.News).Where(c => c.Publish).OrderByDescending(c => c.CreateDate).Take(4).ToList())
{
<div>
@* News title*@
<h4><a href="#">@item.Title</a></h4>
<img src="~/img/news/@item.Image" />
@*Content of lastest news*@
<p>@item.NewsContent</p>
<p><a href="#">@item.Title</a></p>
</div>
}**
}
最后,我想显示部分、美食、新闻作为我的附件照片。 请帮助我再次查看并修复我的代码?非常感谢和感谢。
【问题讨论】:
标签: asp.net-mvc-4 asp.net-mvc-5 linq-to-entities asp.net-mvc-5.2