【问题标题】:Get lastest record from section using mvc5使用 mvc 5 从会话中获取最新记录
【发布时间】: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


    【解决方案1】:

    您可以在类别中添加导航属性,以便轻松访问新闻。

    public partial class Category
    {
        public int Id { get; set; }
        public int SectionId { get; set; }
        ...
    
        public virtual List<News> News { get; set; }    
    }
    

    并选择部分的最新消息:

    @foreach (var cate in sect.SelectMany(s=>s.Categories.SelectMany(c=>c.News))
    .OrderByDescending(n=>n.ID).Take(5))
     {
      <div>
                // Title of lastest news
                <h3></h3>
                <img src="~/img/...." />
                // Content of lastest news
                <p></p>
            </div>
    }
    

    注意:更正确的方法是在 Controller 中查找最新消息并将结果包含在 ViewModel 中,就像这样:

    public class SomeViewModel
    {
      public IEnumerable<Section> Sections {get;set;}
      public IEnumerable<News> LastNews{get;set;}
    }
    

    在控制器中填充这个模型并传入视图。

    【讨论】:

    • 我认为有错误:“xx.Models.Section”不包含“SelectAll”的定义,并且没有扩展方法“Select All”接受“xx.Models”类型的第一个参数。可以找到“部分”(您是否缺少 using 指令或程序集引用?
    • @giathienphu 抱歉,将 SelectAll 更改为 SelectMany
    • 现在一切正常,为部分选择最后一条新闻是 foreach (var cate in sect.Categories.SelectMany(c => c.News).OrderByDescending(c => c.ID).Take( 4){ ....}。非常感谢@awesome
    猜你喜欢
    • 2022-10-01
    • 2015-10-09
    • 2013-06-14
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多