ASP.NET MVC學習筆記-Controller與View傳值

在asp.net2.0的網頁開發模式下,我們一般通過直將訪問頁面控件, 將值寫入到頁面, 但在Asp.net MVC模式下,已不能在Controller中再去訪問頁面控件了,要如何才能給View頁面傳值呢?

在Controller中有兩個字典(ViewData和TempData)來實現View之間的值傳遞, 在ControllerBase類聲明中我們就可以看到:
  1. namespace System.Web.Mvc  
  2. {  
  3.     // 摘要:  
  4.     //     Represents the base class for all MVC controllers.  
  5.     public abstract class ControllerBase : MarshalByRefObject, IController  
  6.     {  
  7.         protected ControllerBase();  
  8.         // 摘要:  
  9.         //     Gets or sets the controller context.  
  10.         public ControllerContext ControllerContext { getset; }  
  11.         //  
  12.         // 摘要:  
  13.         //     Gets or sets the temporary data.  
  14.         public TempDataDictionary TempData { getset; }  
  15.         //  
  16.         // 摘要:  
  17.         //     Gets or sets a value indicating whether the request is valid.  
  18.         public bool ValidateRequest { getset; }  
  19.         //  
  20.         // 摘要:  
  21.         //     Gets or sets the value provider.  
  22.         public IDictionary<string, ValueProviderResult> ValueProvider { getset; }  
  23.         //  
  24.         // 摘要:  
  25.         //     Gets or sets the view data.  
  26.         public ViewDataDictionary ViewData { getset; }  
  27.         // 摘要:  
  28.         //     Executes the specified request context.  
  29.         //  
  30.         // 參數:  
  31.         //   requestContext:  
  32.         //     The request context.  
  33.         protected virtual void Execute(RequestContext requestContext);  
  34.         //  
  35.         // 摘要:  
  36.         //     Executes the core.  
  37.         protected abstract void ExecuteCore();  
  38.         //  
  39.         // 摘要:  
  40.         //     Initializes the specified request context.  
  41.         //  
  42.         // 參數:  
  43.         //   requestContext:  
  44.         //     The request context.  
  45.         protected virtual void Initialize(RequestContext requestContext);  
  46.     }  
  47. }   
而在ViewResultBase中我們同樣可以看到它也含有這兩個字典存在:
  1. // 摘要:  
  2. //     Base class used to supply the model to the view and then render the view  
  3. //     to the response.  
  4. public abstract class ViewResultBase : ActionResult  
  5. {  
  6.     protected ViewResultBase();  
  7.     // 摘要:  
  8.     //     Gets or sets the System.Web.Mvc.TempDataDictionary for this result.  
  9.     public TempDataDictionary TempData { getset; }  
  10.     //  
  11.     // 摘要:  
  12.     //     Gets or sets the System.Web.Mvc.IView that is rendered to the response.  
  13.     public IView View { getset; }  
  14.     //  
  15.     // 摘要:  
  16.     //     Gets or sets the view data System.Web.Mvc.ViewDataDictionary for this result.  
  17.     public ViewDataDictionary ViewData { getset; }  
  18.     //  
  19.     // 摘要:  
  20.     //     Gets or sets the view engines (System.Web.Mvc.ViewEngineCollection) associated  
  21.     //     with this result.  
  22.     public ViewEngineCollection ViewEngineCollection { getset; }  
  23.     //  
  24.     // 摘要:  
  25.     //     Gets or sets the name of the view to be rendered.  
  26.     public string ViewName { getset; }  
  27.     // 摘要:  
  28.     //     When called by the action invoker, renders the view to the response.  
  29.     //  
  30.     // 參數:  
  31.     //   context:  
  32.     //     The context within which the result is executed.  
  33.     public override void ExecuteResult(ControllerContext context);  
  34.     //  
  35.     // 摘要:  
  36.     //     When overridden, returns the System.Web.Mvc.ViewEngineResult used to render  
  37.     //     the view.  
  38.     //  
  39.     // 參數:  
  40.     //   context:  
  41.     //     The context.  
  42.     //  
  43.     // 傳回:  
  44.     //     The view engine.  
  45.     protected abstract ViewEngineResult FindView(ControllerContext context);  
  46. }  
由此可以看出,Controller通過ViewData,TempData傳通到ViewResult中, 然後再由ViewResult傳遞到ViewPage中來實現值傳遞的。
1.TempData和ViewData的應用
ViewData只對當前Action有效,而TempData有點類似於Session, 可在所有View訪問, 一般用於記錄錯誤信息.
Action代碼:
  1. public ActionResult Index()  
  2. {  
  3.     ViewData["Message"] = "Welcome to ASP.NET MVC!";  
  4.     return View();  
  5. }  
頁面代碼:
  1. <h2><%= Html.Encode(ViewData["Message"]) %></h2>  

TempData使用方式與View使用方式一致.
2. ViewData與TextBox實現自動綁定
利用HtmlHelper創建TextBox時,使用名稱與ViewData中的Key一致, 就會自動實現值綁定,如:
  1. Name:<%= Html.TextBox("name") %>  
名稱不相同的情況下,也可以利用TextBox的重載傳值:
  1. Name:<%= Html.TextBox("name", ViewData["Nm"]) %>  
3.View向Controller傳值
1). 利用Action參數
  1. <form name="form1" action="/Home/Index" method="post">  
  2.     Name:<input type="text" name="name" /><br />  
  3.     Sex: <input type="text" name="sex" />  
  4.     <input type="submit" value="submit" />  
  5. </form>  
  6. <%  
  7.     if (ViewData["name"] != null)  
  8.     {  
  9.         Response.Write("your name is:" + ViewData["name"] + ",  your sex is:" + ViewData["sex"]);  
  10.     }             
  11. %>  
  12. :  
  13. public ActionResult Index(string name, string sex)  
  14. {  
  15.     ViewData["Message"] = "Welcome to ASP.NET MVC!";  
  16.     ViewData["name"] = name;  
  17.     ViewData["sex"] = sex;  
  18.     return View();  
  19. }  
2).利用Request.From或Request.QueryString
  1. public ActionResult Index()  
  2. {  
  3.     ViewData["Message"] = "Welcome to ASP.NET MVC!";  
  4.     ViewData["name"] = Request.Form["name"];  
  5.     ViewData["sex"] = Request.Form["sex"];  
  6.     return View();  
  7. }  
3). 利用FormCollection獲取頁面值
  1. public ActionResult Index(FormCollection form)  
  2. {  
  3.     ViewData["Message"] = "Welcome to ASP.NET MVC!";  
  4.     User u=new User();  
  5.     u.Name = form["Name"];  
  6.     u.Password = form["Password"];  
  7.     return View(u);  
  8. }  
4.傳遞強類型
1).添加一個傳遞強類型Model的Action
  1. public ActionResult ModelDemo()  
  2.         {  
  3.             User u= new User() { UserName="li", Password="abcde" };  
  4.             return View(u);  
  5.         }  
對應的View也需要繼隨於ViewPage<User>, 對應代碼如下:
  1. <p>  
  2. <%User u = (User)ViewData.Model;%>  
  3.             UserName:   
  4.             <%= Html.Encode(u.UserName) %>  
  5.         </p>  
  6.         <p>  
  7.             Password:   
  8.             <%= Html.Encode(u.Password) %>  
  9.         </p 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章