c# - 如何在页面中查看此特定数据?
问题描述
我正在尝试建立一个博客网站,如下所示:
类别模型
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>
你能帮我解决这个问题吗?
解决方案
要回答您的一个问题 - 选择第一个条目,然后您可以尝试其余的
@foreach (var item in Model.Take(1))
第一行,然后
@foreach (var item in Model.Skip(1))
对于其余的。它假定模型中的数据已正确排序。
推荐阅读
- javascript - 从参数中使用相同名称的函数访问 ES 模块变量
- botframework - 调用 Direct Line API 生成令牌时出现“Site Missing”错误
- oracle - 如何从大型 sql 文件中导入数据库 Oracle
- python - 如何检测特定对象 ID 的性别?
- dependencies - OSError caffe2_detectron_ops_gpu PyTorch
- r - Plotly:如何使用 R 和 Shiny 为子图创建单个 y 轴标签?
- android - com.sun.jdi.VMDisconnectedException 在调试 azure 媒体服务 android
- dialogflow-es - Google Assistant Action Dialogflow 集成不再起作用
- javascript - 如何使用带有产量的递归?
- jquery - 如何使用 jquery 将 twitter-feed 数据主题切换到暗/亮模式?