asp.net mvc同一个view展示多个不同列表思路

asp.net mvc一个模型一个view容易展示,可是遇到像首页那样,要同时调用好几个不同表的内容一小部分展示时,该怎么是好呢?

下边根据我的测试,用的是mvc access数据测试

先建立一个强类型 类文件lview(文件名自定)

public class lview
{
public List<Article> articles { get; private set; }
public List<Article> articles2 { get; private set; }

public lview(List<Article> larticles, List<Article> lartlist2s)
{
this.articles = larticles;
this.articles2 = lartlist2s;
}
}

 

 

类Article

public class Article
{
public string title { get; set; }
public string neirong { get; set; }
}

 

 

控制器读取index

List<Article> articles = new List<Article>();
List<Article> articles2 = new List<Article>();
// GET: Articleadd
public ActionResult Index()
{
DB mydb = new DB(); //数据库类文件自己写,连接access
OleDbDataReader dr=mydb.GetdataReader("select top 5 * from article");
while (dr.Read())
{
Article article = new Article();
article.title = dr["title"].ToString();
article.neirong= dr["neirong"].ToString();
articles.Add(article);
}
OleDbDataReader dr2 = mydb.GetdataReader("select top 5 * from article");
while (dr2.Read())
{
Article article2 = new Article();
article2.title = dr2["title"].ToString();
article2.neirong = dr2["neirong"].ToString();
articles2.Add(article2);
}
mydb.close();
//lview lv = new lview(articles,articles2);
//return View(lv);

return View(new lview(articles, articles2));

 

 

view页面读取

@model WebApplication40.Controllers.lview //(引用这个多的模型类)

 

下面循环读取1

<table class="table">

@foreach (var item in Model.articles)  //(Model.xx)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.title)
</td>
<td>
@Html.DisplayFor(modelItem => item.neirong)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}

</table>

循环读取2

<table class="table">

@foreach (var item in Model.articles2)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.title)
</td>
<td>
@Html.DisplayFor(modelItem => item.neirong)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}

</table>

  

如果还有很多,就@foreach (var item in Model.xxx)

posted @ 2020-09-08 11:42  一只奋斗的小小鸟  阅读(324)  评论(0编辑  收藏  举报