ASP.NET MVC中在Action獲取提交的表單數據方法總結

有Index視圖如下:


視圖代碼如下:

[html] view plaincopy
  1. <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>  
  2.   
  3. <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">  
  4.     主頁  
  5. </asp:Content>  
  6.   
  7. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">  
  8.   
  9.     <h2><%= Html.Encode(ViewData["Message"]) %></h2>  
  10.     <br />  
  11.     <br />  
  12.   
  13.     <% using(Html.BeginForm("HandleForm", "Home")) %>  
  14.     <% { %>  
  15.         Enter your name: <%= Html.TextBox("name") %>  
  16.         <br /><br />  
  17.         Select your favorite color:<br />  
  18.         <%= Html.RadioButton("favColor", "Blue", true) %> Blue <br />  
  19.         <%= Html.RadioButton("favColor", "Purple", false)%> Purple <br />  
  20.         <%= Html.RadioButton("favColor", "Red", false)%> Red <br />  
  21.         <%= Html.RadioButton("favColor", "Orange", false)%> Orange <br />  
  22.         <%= Html.RadioButton("favColor", "Yellow", false)%> Yellow <br />  
  23.         <%= Html.RadioButton("favColor", "Brown", false)%> Brown <br />  
  24.         <%= Html.RadioButton("favColor", "Green", false)%> Green   
  25.         <br /><br />  
  26.         <%= Html.CheckBox("bookType") %> I read more fiction than non-fiction.<br />  
  27.         <br /><br />  
  28.         My favorite pet: <%= Html.DropDownList("pets") %>  
  29.         <br /><br />  
  30.         <input type="submit" value="Submit" />  
  31.     <% } %>  
  32.   
  33. </asp:Content>  

如圖填寫表單數據:

分別使用不同的表單處理方法,對提交的表單數據在視圖FormResults呈現。

提交表單對應的HomeController,包含以不同方法獲取表單數據的代碼,如下:

[csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace HtmlHelper.Controllers  
  8. {  
  9.     [HandleError]  
  10.     public class HomeController : Controller  
  11.     {  
  12.         public ActionResult Index()  
  13.         {  
  14.             ViewData["Message"] = "歡迎使用 ASP.NET MVC!";  
  15.   
  16.             //手動構造頁面中下拉框的寵物數據  
  17.             List<string> petList = new List<string>();  
  18.             petList.Add("Dog");  
  19.             petList.Add("Cat");  
  20.             petList.Add("Hamster");  
  21.             petList.Add("Parrot");  
  22.             petList.Add("Gold fish");  
  23.             petList.Add("Mountain lion");  
  24.             petList.Add("Elephant");  
  25.   
  26.             ViewData["Pets"] = new SelectList(petList);  
  27.   
  28.             return View();  
  29.         }  
  30.   
  31.         public ActionResult About()  
  32.         {  
  33.             return View();  
  34.         }  
  35.   
  36.         /// <summary>  
  37.         /// 處理表單提交數據,方法1:使用傳統的Request請求取值  
  38.         /// </summary>  
  39.         /// <returns></returns>  
  40.         public ActionResult HandleForm()  
  41.         {  
  42.             ViewData["name"] = Request["name"];  
  43.             ViewData["favColor"] = Request["favColor"];  
  44.             ViewData["bookType"] = Request["bookType"];  
  45.             ViewData["pet"] = Request["pets"];  
  46.   
  47.             return View("FormResults");  
  48.         }  
  49.   
  50.         /// <summary>  
  51.         /// 處理表單提交數據,方法2:Action參數名與表單元素name值一一對應  
  52.         /// </summary>  
  53.         /// <param name="name"></param>  
  54.         /// <param name="favColor"></param>  
  55.         /// <param name="bookType"></param>  
  56.         /// <param name="pets"></param>  
  57.         /// <returns></returns>  
  58.         //public ActionResult HandleForm(string name, string favColor, Boolean bookType, string pets)  
  59.         //{  
  60.         //    ViewData["name"] = name;  
  61.         //    ViewData["favColor"] = favColor;  
  62.         //    ViewData["bookType"] = bookType;  
  63.         //    ViewData["pet"] = pets;  
  64.   
  65.         //    return View("FormResults");  
  66.         //}  
  67.   
  68.         /// <summary>  
  69.         /// 處理表單提交數據,方法3:從MVC封裝的FormCollection容器中讀取  
  70.         /// </summary>  
  71.         /// <param name="form"></param>  
  72.         /// <returns></returns>  
  73.         //public ActionResult HandleForm(FormCollection form)  
  74.         //{  
  75.         //    ViewData["name"] = form["name"];  
  76.         //    ViewData["favColor"] = form["favColor"];  
  77.         //    ViewData["bookType"] = form["bookType"];  
  78.         //    ViewData["pet"] = form["pets"];  
  79.   
  80.         //    return View("FormResults");  
  81.         //}  
  82.   
  83.         /// <summary>  
  84.         /// 處理表單提交數據,方法4:使用實體作爲Action參數傳入,前提是提交的表單元素名稱與實體屬性名稱一一對應  
  85.         /// </summary>  
  86.         /// <param name="request"></param>  
  87.         /// <returns></returns>  
  88.         //[HttpPost]  
  89.         //public ActionResult HandleForm(InforModel infor)  
  90.         //{  
  91.         //    ViewData["name"] = infor.name;  
  92.         //    ViewData["favColor"] = infor.favColor;  
  93.         //    ViewData["bookType"] = infor.bookType;  
  94.         //    ViewData["pet"] = infor.pets;  
  95.   
  96.         //    return View("FormResults");  
  97.         //}  
  98.   
  99.     }  
  100. }  

在FormResults視圖顯示ViewData的數據,如圖所示:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章