MVC3 Linq选择每个类别的前5名

时间:2012-06-16 14:30:03

标签: asp.net asp.net-mvc-3 linq

我试图在局部视图中选择并显示每个新闻类别的最新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>
}

HomeNewzCat模型

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; }
    }    
}

1 个答案:

答案 0 :(得分:1)

首先,将<ul>标记移到foreach循环之外,并添加检查是否没有返回记录

@if(Model != null) {
    <ul>
        @foreach(var item in Model) {
           <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>
}

接下来,我们需要构建一个HomeNewzCat列表以发送到部分视图

public ActionResult _HomeCatNews()
{
    var info = db.News
        .GroupBy(r => r.CatId)
        .SelectMany(g => g.OrderBy(r => r.RevDate)
        .Take(5))
        .ToList();

    var model = new List<HomeNewzCat>();
    HomeNewzCat record = null;

    // first make sure we have some records
    if(info.Count() > 0) {
        foreach(var item in info) {
            // you will need to modify these item.Field to match your database names
            record = new HomeNewzCat 
                         {
                             CategoryID = item.CatID,
                             Category = item.Category,
                             RevisionDate = item.RevDate,
                             NewsTitle = item.NewsTitle,
                             NewsPic = item.NewsPic,
                             NewsContent = item.NewsContent,
                             NewsSource = item.NewsSource,
                             Date = item.Date
                         };
            model.Add(record);
        }
    }
    return PartialView("_HomeCatNewz", model);
}