首页 > 解决方案 > 如何在页面中查看此特定数据?

问题描述

我正在尝试建立一个博客网站,如下所示:

类别模型

public class Category
    {
        public int CategoryID { get; set; }
        public String CategoryName { get; set; }
        public virtual List<Article> Articles { get; set; }
    }

文章模型

public class Article
    {
        public int Id { get; set; }
        public string Header { get; set; }
        public string Content { get; set; }
        public byte[] BlogImage{ get; set; }
        public DateTime? CreationDate{ get; set; }
        public int? CategoryID{ get; set; }
        public virtual Category Category { get; set; }
    }

那么,我怎样才能在 ActionResult 函数中发送数据并在页面中查看博客卡片

public ActionResult Index()
        {
            using (MvcBlogContext context = new MvcBlogContext())
            {
                 // how to code here?
            }
        }
<div class="row">
     @foreach (var item in Model)
     {
           <div class="col-md-4">
            // how to view category name here?
           </div>
     }
</div>

<div class="row">
     @foreach (var item in Model)
     {
           <div class="col-md-4">
                <div class="post post-widget"
                     // how to view from 2nd to 4th blog here?
                </div>
           </div>
     }
</div>

你能帮我解决这个问题吗?

标签: c#asp.net-mvcmodel-view-controller

解决方案


要回答您的一个问题 - 选择第一个条目,然后您可以尝试其余的

@foreach (var item in Model.Take(1))

第一行,然后

@foreach (var item in Model.Skip(1))

对于其余的。它假定模型中的数据已正确排序。


推荐阅读