zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    软件测试大赛决赛简讯
    期末提交作业清单
    4月12日-4月19日任务清单
    20160405
    软件系统设计文档模板
    吐槽
    致我亲爱的学生
    HBase 环境搭建
    Zookeeper 环境搭建
    hive 部署
  • 原文地址:https://www.cnblogs.com/shijiehaiyang/p/13631607.html
Copyright © 2011-2022 走看看