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

本文介绍了一个ASP.NET MVC应用中使用的博客模型设计,包括文章(Article)和类别(Cate)两个主要实体,以及如何通过List Model来组织这些数据以供视图层展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[csharp]  view plain copy 在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 plain copy 在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
             }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值