1

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

1 回答 1

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);
}
于 2012-06-16T15:51:11.603 回答