淺析ASP.NET MVC中Controller與View數據傳遞
在ASP.NET MVC中,經常會在Controller與View之間傳遞數據,因此,熟練、靈活的掌握這兩層之間的數據傳遞方法就非常重要。本文從兩個方面進行探討:
#T#
◆Controller向View傳遞數據
◆View向Controller傳遞數據
一、Controller向View傳遞數據
1. 使用ViewData傳遞數據
我們在Controller中定義如下:
- ViewData[“Message”] = “Hello word!”;
 
然后在View中讀取Controller中定義的ViewData數據,代碼如下:
- <% = Html.Encode(ViewData[“Message”]) %>
 
2. 使用TempData傳遞數據
我們在Controller中定義如下:
- TempData[“Message”] = “Hello word!”;
 
然后在View中讀取Controller中定義的TempData數據,代碼如下:
- <% = Html.Encode(TempData [“Message”]) %>
 
3.使用Model傳遞數據
使用Model傳遞數據的時候,通常在創(chuàng)建View的時候我們會選擇創(chuàng)建強類型View如下圖所示:
創(chuàng)建強類型的View以后,View的***行代碼如下所示:
- <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcInduction.Models.People>" %>
 - <MvcInduction.Models.People>
 
就代表了這個View使用的Model為“MvcInduction.Models.People”
總結:
1. ViewData與TempData方式是弱類型的方式傳遞數據,而使用Model傳遞數據是強類型的方式。
2. ViewData與TempData是完全不同的數據類型,ViewData數據類型是ViewDataDictionary類的實例化對象,而TempData的數據類型是TempDataDictionary類的實例化對象。
3. TempData實際上保存在Session中,控制器每次執(zhí)行請求時都會從Session中獲取TempData數據并刪除該Session。TempData數據只能在控制器中傳遞一次,其中的每個元素也只能被訪問一次,訪問之后會被自動刪除。
4. ViewData只能在一個Action方法中進行設置,在相關的視圖頁面讀取,只對當前視圖有效。理論上,TempData應該可以在一個Action中設置,多個頁面讀取。但是,實際上TempData中的元素被訪問一次以后就會被刪除。
二、View向Controller傳遞數據
在ASP.NET MVC中,將View中的數據傳遞到控制器中,主要通過發(fā)送表單的方式來實現。具體的方式有:
1. 通過Request.Form讀取表單數據
我們在View層做如下定義:
- <% using (Html.BeginForm("ActionName", "ControllerName"))
 - { %>
 - UserName:<% Html.TextBox("UserName"); %>
 - Password:<% Html.TextBox("Password"); %>
 - <%} %>
 
注意:ActionName為對應的Action名,ControllerName為對應的Controller名稱
然后在Controller層,通過Request.Form讀取表單數據的代碼如下所示:
- [AcceptVerbs(HttpVerbs.Post)]
 - public ActionResult ActionName()
 - {
 - string username = Request.Form["UserName"];
 - string password = Request.Form["Password"];
 - return View();
 - }
 
2. 通過FormCollection讀取表單數據
我們在View層做如下定義:
- <% using (Html.BeginForm("ActionName", "ControllerName"))
 - { %>
 - UserName:<% Html.TextBox("UserName"); %>
 - Password:<% Html.TextBox("Password"); %>
 - <%} %>
 
然后在Controller層,通過FormCollection讀取表單數據的代碼如下所示:
- [AcceptVerbs(HttpVerbs.Post)]
 - public ActionResult ActionName(FormCollection formCollection)
 - {
 - string username = formCollection["UserName"];
 - string password = formCollection["Password"];
 - return View();
 - }
 
3. 自定義數據綁定
自定義數據綁定的方法如下:創(chuàng)建一個自定義數據綁定類,讓這個類繼承自IModelBinder,實現該接口中的BindModel方法。
由于寫作倉促,代碼未列出。敬請見諒。
總結:雖然我們可以通過Request.Form或FormCollection方式讀取表單數據,可是通常這兩種方式都比較繁瑣,在強類型View的情況下,我們通常會使用Controller 基類的內置方法UpdateModel(),該方法支持使用傳入的表單參數更新對象的屬性,它使用反射機制來解析對象的屬性名稱,接著基于客戶端傳入的參數值自動賦值給對象相關屬性。
以下是我寫的一個Demo的一段使用UpdateModel的代碼例子:
使用UpdateModel()的代碼例子
- [AcceptVerbs(HttpVerbs.Post)]
 - public ActionResult Edit(int id, FormCollection collection)
 - {
 - //Users user = userRepository.GetUser(id);
 - //user.UserName = Request.Form["UserName"];
 - //user.Password = Request.Form["Password"];
 - //user.Telephone = Request.Form["Telephone"];
 - //user.Address = Request.Form["Address"];
 - //上述方法有一點繁瑣,特別是增加異常處理邏輯之后。
 - 一個更好的方法是使用Controller 基類的內置方法UpdateModel()。
 - 該方法支持使用傳入的表單參數更新對象的屬性,它使用反射機制來解析對象的屬性名稱,
 - 接著基于客戶端傳入的參數值自動賦值給對象相關屬性。
 - Users user = userRepository.GetUser(id);
 - string[] allowedProperties = new[] { "UserName", "Password", "Telephone", "Address" };
 - UpdateModel(user, allowedProperties);
 - userRepository.Save();
 - return RedirectToAction("Details", new { id = user.ID });
 - }
 
原文標題:ASP.NET MVC中Controller與View之間的數據傳遞總結
鏈接:http://www.cnblogs.com/wlb/archive/2009/12/10/1621475.html















 
 
 
 
 
 
 