MVC3.0 Razor 单个View页返回多个model实体数据集

来源:互联网 发布:用eclipse编写python 编辑:程序博客网 时间:2024/06/06 10:49
[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. namespace Models  
  2. {  
  3.     public class Articel  
  4.     {  
  5.         public int Id { getset; }  
  6.         [Required]  
  7.         [DisplayName("标题")]  
  8.         [MaxLength(100)]  
  9.         public string Title { getset; }  
  10.     }  
  11.    
  12.     public class Cate  
  13.     {  
  14.         public int CateId { getset; }  
  15.         [DisplayName("文章类别")]  
  16.         [Required]  
  17.         public string CateName { getset; }  
  18.    
  19.         public List<Article> article { getset; }  
  20.     }  
  21.    
  22.     public class ListModel  
  23.     {  
  24.         /// <summary>  
  25.         /// 文章  
  26.         /// </summary>  
  27.         public IList<Article> artcleModel { getset; }  
  28.    
  29.         /// <summary>  
  30.         /// 文章类别  
  31.         /// </summary>  
  32.         public IList<Cate> cateModel { getset; }  
  33.    
  34.     }  
  35. }  
  36.    
[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <div>  
  2. </div><span style="font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; line-height: 21px; orphans: 2; white-space: pre-wrap; widows: 2; background-color: rgb(244, 244, 244);">Action方法编写如下:</span>  

public ActionResult Index()
        {
            var model = new ListModel();
            model.artcleModel= Articel.ToList();
            model.cateModel= Cate.ToList();
            return View(model);
        }
 
View层引用如下:
@model Models.ListModel
 
@foreach (var tt in Models.artcleModel)
            {
             @tt.Title
            }
 
@foreach (var tt in Models.cateModel)
            {
              @tt.CateName
            }

版权声明:本文为博主原创文章,未经博主允许不得转载。

0 0
原创粉丝点击