MVC3 如何将多个查询结果赋值到cshtml页面中?

Controller代码如下:
public ActionResult Index()
{
var data1 = (from t in db.Article
where t.Type == "考试动态"
select t).Take(5).ToList();

var data2 = (from t in db.Article
where t.Type == "培训教材"
select t).Take(5).ToList();

return View();
}

我想从数据库中的“考试动态”和"培训教材“各取出5条记录,并将取出来的记录分别显示在前台Index.cshtml页面的两个<div class="listBox"></div>框中,请问Controller代码中如何将查询结果赋值到Index.cshtml页面中,以及Index.cshtml中的循环输出代码如何写?

单个查询的赋值可以这样写,如下:
public ActionResult Index()
{
viewData.Model = (from t in db.Article
where t.Type == "培训教材"
select t).Take(5).ToList();

return View();
}
Index.cshtml代码:
@foreach (var item in Model)
{
@Html.ActionLink(@item.Title, "Details", "Article", new { id = @item.Id }, null)
}

第1个回答  2012-10-25
自定义一个实体类,带两个属性,页面上转换Model到这个实体类。
第2个回答  2012-10-25
自定义一个实体类,带两个属性,页面上转换Model到这个实体类不就好了本回答被提问者和网友采纳
相似回答