MVC3 如何将多个查询结果赋值到cshtml页面中?
Controller代码如下:publicActionResultIndex(){vardata1=(fromtindb.Articlewheret.Type=="考试动...
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)
} 展开
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)
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询