日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
淺析ASP.NETMVC中Controller與View數(shù)據(jù)傳遞

在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ù)

我們在Controller中定義如下:

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

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

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

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

我們在Controller中定義如下:

 
 
  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" %>

就代表了這個(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í)例化對象,而TempData的數(shù)據(jù)類型是TempDataDictionary類的實(shí)例化對象。

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

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

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

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

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

我們在View層做如下定義:

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

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

然后在Controller層,通過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();
  7. }

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

我們在View層做如下定義:

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

然后在Controller層,通過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方法。
由于寫作倉促,代碼未列出。敬請見諒。

總結(jié):雖然我們可以通過Request.Form或FormCollection方式讀取表單數(shù)據(jù),可是通常這兩種方式都比較繁瑣,在強(qiáng)類型View的情況下,我們通常會(huì)使用Controller 基類的內(nèi)置方法UpdateModel(),該方法支持使用傳入的表單參數(shù)更新對象的屬性,它使用反射機(jī)制來解析對象的屬性名稱,接著基于客戶端傳入的參數(shù)值自動(dòng)賦值給對象相關(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ù)更新對象的屬性,它使用反射機(jī)制來解析對象的屬性名稱,
  12. 接著基于客戶端傳入的參數(shù)值自動(dòng)賦值給對象相關(guān)屬性。
  13.             Users user = userRepository.GetUser(id);
  14.             string[] allowedProperties = new[] { "UserName", "Password", "Telephone", "Address" };
  15.                 UpdateModel(user, allowedProperties);
  16.                 userRepository.Save();
  17.                 return RedirectToAction("Details", new { id = user.ID });
  18.         }

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


標(biāo)題名稱:淺析ASP.NETMVC中Controller與View數(shù)據(jù)傳遞
瀏覽路徑:http://www.5511xx.com/article/cdcicpe.html