自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺析ASP.NET MVC中Controller與View數(shù)據(jù)傳遞

開(kāi)發(fā) 后端
在這里我們將討論的是ASP.NET MVC中Controller與View之間的數(shù)據(jù)傳遞,希望對(duì)大家有所幫助。

在ASP.NET MVC中,經(jīng)常會(huì)在Controller與View之間傳遞數(shù)據(jù),因此,熟練、靈活的掌握這兩層之間的數(shù)據(jù)傳遞方法就非常重要。本文從兩個(gè)方面進(jìn)行探討:

#T#

◆Controller向View傳遞數(shù)據(jù)

◆View向Controller傳遞數(shù)據(jù)

一、Controller向View傳遞數(shù)據(jù)

1. 使用ViewData傳遞數(shù)據(jù)

我們?cè)贑ontroller中定義如下:

  1. ViewData[“Message”] = “Hello word!”; 

然后在View中讀取Controller中定義的ViewData數(shù)據(jù),代碼如下:

  1. <% = Html.Encode(ViewData[“Message”]) %> 

2. 使用TempData傳遞數(shù)據(jù)

我們?cè)贑ontroller中定義如下:

  1. TempData[“Message”] = “Hello word!”; 

然后在View中讀取Controller中定義的TempData數(shù)據(jù),代碼如下:

  1. <% = Html.Encode(TempData [“Message”]) %> 

3.使用Model傳遞數(shù)據(jù)

使用Model傳遞數(shù)據(jù)的時(shí)候,通常在創(chuàng)建View的時(shí)候我們會(huì)選擇創(chuàng)建強(qiáng)類型View如下圖所示:

創(chuàng)建強(qiáng)類型的View以后,View的***行代碼如下所示:

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcInduction.Models.People>" %> 
  2. <MvcInduction.Models.People> 

就代表了這個(gè)View使用的Model為“MvcInduction.Models.People”

總結(jié):

1. ViewData與TempData方式是弱類型的方式傳遞數(shù)據(jù),而使用Model傳遞數(shù)據(jù)是強(qiáng)類型的方式。

2. ViewData與TempData是完全不同的數(shù)據(jù)類型,ViewData數(shù)據(jù)類型是ViewDataDictionary類的實(shí)例化對(duì)象,而TempData的數(shù)據(jù)類型是TempDataDictionary類的實(shí)例化對(duì)象。

3. TempData實(shí)際上保存在Session中,控制器每次執(zhí)行請(qǐng)求時(shí)都會(huì)從Session中獲取TempData數(shù)據(jù)并刪除該Session。TempData數(shù)據(jù)只能在控制器中傳遞一次,其中的每個(gè)元素也只能被訪問(wèn)一次,訪問(wèn)之后會(huì)被自動(dòng)刪除。

4.         ViewData只能在一個(gè)Action方法中進(jìn)行設(shè)置,在相關(guān)的視圖頁(yè)面讀取,只對(duì)當(dāng)前視圖有效。理論上,TempData應(yīng)該可以在一個(gè)Action中設(shè)置,多個(gè)頁(yè)面讀取。但是,實(shí)際上TempData中的元素被訪問(wèn)一次以后就會(huì)被刪除。

二、View向Controller傳遞數(shù)據(jù)

在ASP.NET MVC中,將View中的數(shù)據(jù)傳遞到控制器中,主要通過(guò)發(fā)送表單的方式來(lái)實(shí)現(xiàn)。具體的方式有:

1. 通過(guò)Request.Form讀取表單數(shù)據(jù)

我們?cè)赩iew層做如下定義:

  1. <% using (Html.BeginForm("ActionName", "ControllerName"))  
  2.        { %> 
  3.     UserName:<% Html.TextBox("UserName"); %> 
  4.     Password:<% Html.TextBox("Password"); %> 
  5. <%} %> 

注意:ActionName為對(duì)應(yīng)的Action名,ControllerName為對(duì)應(yīng)的Controller名稱

然后在Controller層,通過(guò)Request.Form讀取表單數(shù)據(jù)的代碼如下所示:

  1. [AcceptVerbs(HttpVerbs.Post)]  
  2.         public ActionResult ActionName()  
  3.         {  
  4.             string username = Request.Form["UserName"];  
  5.             string password = Request.Form["Password"];  
  6.             return View();  

2. 通過(guò)FormCollection讀取表單數(shù)據(jù)

我們?cè)赩iew層做如下定義:

  1. <% using (Html.BeginForm("ActionName", "ControllerName"))  
  2.        { %> 
  3.     UserName:<% Html.TextBox("UserName"); %> 
  4.     Password:<% Html.TextBox("Password"); %> 
  5. <%} %> 

然后在Controller層,通過(guò)FormCollection讀取表單數(shù)據(jù)的代碼如下所示:

  1. [AcceptVerbs(HttpVerbs.Post)]  
  2.         public ActionResult ActionName(FormCollection formCollection)  
  3.         {  
  4.             string username = formCollection["UserName"];  
  5.             string password = formCollection["Password"];  
  6.             return View();  
  7.         } 

3.  自定義數(shù)據(jù)綁定

自定義數(shù)據(jù)綁定的方法如下:創(chuàng)建一個(gè)自定義數(shù)據(jù)綁定類,讓這個(gè)類繼承自IModelBinder,實(shí)現(xiàn)該接口中的BindModel方法。
由于寫作倉(cāng)促,代碼未列出。敬請(qǐng)見(jiàn)諒。

總結(jié):雖然我們可以通過(guò)Request.Form或FormCollection方式讀取表單數(shù)據(jù),可是通常這兩種方式都比較繁瑣,在強(qiáng)類型View的情況下,我們通常會(huì)使用Controller 基類的內(nèi)置方法UpdateModel(),該方法支持使用傳入的表單參數(shù)更新對(duì)象的屬性,它使用反射機(jī)制來(lái)解析對(duì)象的屬性名稱,接著基于客戶端傳入的參數(shù)值自動(dòng)賦值給對(duì)象相關(guān)屬性。

以下是我寫的一個(gè)Demo的一段使用UpdateModel的代碼例子:

使用UpdateModel()的代碼例子

  1. [AcceptVerbs(HttpVerbs.Post)]  
  2.         public ActionResult Edit(int id, FormCollection collection)  
  3.         {  
  4.             //Users user = userRepository.GetUser(id);  
  5.             //user.UserName = Request.Form["UserName"];  
  6.             //user.Password = Request.Form["Password"];  
  7.             //user.Telephone = Request.Form["Telephone"];  
  8.             //user.Address = Request.Form["Address"];  
  9.             //上述方法有一點(diǎn)繁瑣,特別是增加異常處理邏輯之后。
  10. 一個(gè)更好的方法是使用Controller 基類的內(nèi)置方法UpdateModel()。
  11. 該方法支持使用傳入的表單參數(shù)更新對(duì)象的屬性,它使用反射機(jī)制來(lái)解析對(duì)象的屬性名稱,
  12. 接著基于客戶端傳入的參數(shù)值自動(dòng)賦值給對(duì)象相關(guān)屬性。  
  13.             Users user = userRepository.GetUser(id);  
  14.             string[] allowedProperties = new[] { "UserName""Password""Telephone""Address" };  
  15.                 UpdateModel(user, allowedProperties);  
  16.                 userRepository.Save();  
  17.  
  18.                 return RedirectToAction("Details"new { id = user.ID });  
  19.         } 

原文標(biāo)題:ASP.NET MVC中Controller與View之間的數(shù)據(jù)傳遞總結(jié)

鏈接:http://www.cnblogs.com/wlb/archive/2009/12/10/1621475.html

責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2009-07-31 12:43:59

ASP.NET MVC

2009-07-23 14:31:20

ASP.NET MVC

2009-04-08 09:58:07

ASP.NET MVCTempData框架

2011-04-14 09:19:22

ASP.NET MVC

2009-07-20 10:33:02

ASP.NET MVC

2009-09-24 09:26:22

ASP.NET MVC

2011-07-13 09:31:48

ASP.NET數(shù)據(jù)傳遞

2009-12-02 09:07:45

ASP.NET 4.0

2009-07-24 15:47:35

ASP.NET與ASP

2010-03-26 09:16:44

2015-06-17 17:01:48

ASP.NET

2009-12-16 09:16:53

ASP.NET頁(yè)面間數(shù)

2010-03-19 09:17:16

ASP.NET MVC

2009-08-03 18:35:51

ASP.NET數(shù)據(jù)緩存

2009-11-02 10:15:53

ASP.NET MVC

2009-07-30 13:45:40

ASP.NET開(kāi)發(fā)模式MVC模式

2009-12-17 15:07:16

ASP.NET MVC

2009-07-24 13:20:44

MVC框架ASP.NET

2009-07-24 13:41:15

ASP.NET AJA

2009-08-05 18:36:12

ASP.NET Che
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)