ASP.NET MVC Controller向View传值的几种方式

 上几篇博文提到MVC和WebForm的区别,主要是MVC的Controller和View将传统的WebForm的窗体和后台代码做了解耦,这篇博文简单介绍一下在MVC中Controller向View是如何传值的。

    有点MVC基础的都知道,Controller向View传值主要有四种途径,下面将一一介绍。

 

    1)ViewBag

 

    ViewBag是动态类型,使用时直接添加属性赋值即可 ViewBag.myName

    控制器代码:

      

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public ActionResult Index()  
  2. {  
  3.     ViewBag.name = "周江霄";  
  4.     ViewBag.message = "欢迎使用MVC设计模式~~";  
  5.     return View();  
  6. }  

           

    视图代码:

 

      

[javascript] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <div>          
  2.     <!--利用HtmlHelper创建TextBox时,使用名称与ViewBag中的Key一致, 就会自动实现值绑定-->  
  3.     @Html.TextBox("name")  
  4.     @ViewBag.message          
  5. </div>  

       

    效果显示:

 

      

 

        2)ViewData

 

     ViewData只对当前Action有效,它是一个字典集合,通过key值读取对应的value;

     控制器代码:

  

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public ActionResult Index()  
  2.         {  
  3.             ViewData["name"] = "周江霄";  
  4.             ViewData["message"] = "欢迎使用MVC设计模式~~";  
  5.             return View();  
  6.         }  

     

      视图代码:

 

  

[javascript] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <div>          
  2.         <!--利用HtmlHelper创建TextBox时,使用名称与ViewData中的Key一致, 就会自动实现值绑定-->  
  3.         @Html.TextBox("name")  
  4.         @ViewData["message"]          
  5.     </div>  

           

         效果显示:

 

  

 

  3)TempData

 

    使用TempData和使用ViewData方法是一样的,但是它可用于在不同的Action之间传值,这是ViewData做不到的。

   控制器代码:

   

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public class MVCController : Controller  
  2. {     
  3.     public ActionResult Index1()  
  4.     {  
  5.         TempData["name"] = "周江霄";              
  6.         return View();  
  7.     }  
  8.   
  9.     public ActionResult Index2()  
  10.     {  
  11.         string strName = TempData["name"].ToString()  
  12.         return View();  
  13.     }  

      

    上面的代码中,Index()给TempData添加了一个键值对,假设我们先请求Index这个Action,接着请求Index2这个Action,那么在Index2中,我们便可以得到之前添加到TempData的键值对。有趣的是,这时如果再次请求Index2,那么从TempData中读到的MyName的值会是null。这是因为TempData和一个临时的Session差不多,当Acion执行的时候它做为一个全局对象保存的内存中,而一旦Action的执行完成,就会释放内存空间,这就是它与ViewData最大的不同之处。  

 

 

       4)Model

 

     Controller通过Model传值应该是MVC中使用最为普遍的一种传值方式,通过强类型绑定,在View中可以很方便的通过Model的相应属性得到想要的值。

     Model代码:

        

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public partial class YzAdministratorEntity  
  2. {  
  3.     public YzAdministratorEntity()  
  4.     {  
  5.         this.YzClearRecordEntity = new HashSet<YzClearRecordEntity>();  
  6.         this.YzNotifyInfoEntity = new HashSet<YzNotifyInfoEntity>();  
  7.     }  
  8.   
  9.       
  10.     public System.Guid ID { get; set; }  
  11.     public string AdminPassword { get; set; }  
  12.     public string AdminName { get; set; }  
  13.     public bool isUsed { get; set; }  
  14.   
  15.     [Required(AllowEmptyStrings = false, ErrorMessage = "用户ID不能为空")]  
  16.     public string AdministratorID { get; set; }  
  17.     public string AdminLevel { get; set; }  
  18.   
  19.     public virtual ICollection<YzClearRecordEntity> YzClearRecordEntity { get; set; }  
  20.     public virtual ICollection<YzNotifyInfoEntity> YzNotifyInfoEntity { get; set; }  
  21. }  

            

    视图代码:

 

    强类型绑定:        

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. @model Model.YzAdministratorEntity  


       通过Linq自动匹配Model和Label或TxtBox的属性值:

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
    1. <div>  
    2.     <span class="editor-label">  
    3.         @Html.Label("编号:")  
    4.     </span>  
    5.     <span class="editor-field">  
    6.         @Html.EditorFor(a => a.AdministratorID)  
    7.         @Html.ValidationMessageFor(model => model.AdministratorID)  
    8.     </span>  
    9. </div>  
    10.   
    11. <div>  
    12.     <span class="editor-label">  
    13.         @Html.Label("密码:")  
    14.     </span>  
    15.     <span class="editor-field">  
    16.         @Html.EditorFor(a => a.AdminPassword )  
    17.     </span>  
    18. </div>  
    19.   
    20. <div>  
    21.     <span class="editor-label">  
    22.         @Html.Label("真实姓名:")  
    23.     </span>  
    24.     <span class="editor-field">  
    25.         @Html.EditorFor(a => a.AdminName )  
    26.     </span>  
    27. </div>  
posted @ 2016-11-19 17:39  在西天取经的路上……  阅读(119)  评论(0编辑  收藏  举报