asp.net MVC中的@model与Model

asp.net MVC中的@model与Model

 

https://blog.csdn.net/ydm19891101/article/details/44301201html

 

在MVC的实际使用中,咱们常常须要在先后台之间传递数据,这也就是Model实体的使用。传递数据的方式还有ViewBag和ViewData两种。具体两种方式的区别于使用请参考 MVC学习系列-ViewData与ViewBag,今天主要讲解的是 MVC中的@model与Model的使用。asp.net

(1)定义Model实体学习

 

[csharp]  view plain  copy
 
  1. public class SearchWithFundingList  
  2.     {  
  3.         /// <summary>  
  4.         /// 方案分类  
  5.         /// </summary>  
  6.         public int ProjectCategory { get; set; }  
  7.         /// <summary>  
  8.         /// 发起时间小  
  9.         /// </summary>  
  10.         public string MinAddDate { get; set; }  
  11.         /// <summary>  
  12.         /// 发起时间大  
  13.         /// </summary>  
  14.         public string MaxAddDate { get; set; }  
  15.         /// <summary>  
  16.         /// 状态  
  17.         /// </summary>  
  18.         public int State { get; set; }  
  19.         /// <summary>  
  20.         /// 昵称  
  21.         /// </summary>  
  22.         public string NickName { get; set; }  
  23.         /// <summary>  
  24.         /// 用户id  
  25.         /// </summary>  
  26.         public int Mid { get; set; }  
  27.     }  
(2)传递Model

 

 

[csharp]  view plain  copy
 
  1. private SearchWithFundingList GetFormWithFundingNow(int id, int uid)  
  2. {  
  3.     SearchWithFundingList model = new SearchWithFundingList();  
  4.     model.Mid = uid;  
  5.     model.State = WithFundingStateKey.Doing;  
  6.     model.ProjectCategory = id;  
  7.     return model;  
  8. }  

 

注意:必定要在最后return 实体,否则前台的Model实体是null ui

(3)具体调用

在页面代码最上面添加上实体的声明spa

 

[html]  view plain  copy
 
  1. @{Layout = null;}  
  2. @model StockFunds.Entities.DTO.SearchWithFundingList  
接下来就能够在页面里使用Model(这里的实体就是指SearchWithFundingList实体),而且此时的Model已是强类型了,咱们能够点出具体的属性,很是方便

 

 

[html]  view plain  copy
 
  1. <span class="state">Model.State</span>元</span>  
相关文章
相关标签/搜索