生活随笔
收集整理的這篇文章主要介紹了
ASP.NET MVC中在Action获取提交的表单数据方法总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有Index視圖如下:
視圖代碼如下:
[html] view plaincopy
<%@?Page?Language="C#"?MasterPageFile="~/Views/Shared/Site.Master"?Inherits="System.Web.Mvc.ViewPage"?%>????<asp:Content?ID="Content1"?ContentPlaceHolderID="TitleContent"?runat="server">??????主頁??</asp:Content>????<asp:Content?ID="Content2"?ContentPlaceHolderID="MainContent"?runat="server">????????<h2><%=?Html.Encode(ViewData["Message"])?%></h2>??????<br?/>??????<br?/>????????<%?using(Html.BeginForm("HandleForm",?"Home"))?%>??????<%?{?%>??????????Enter?your?name:?<%=?Html.TextBox("name")?%>??????????<br?/><br?/>??????????Select?your?favorite?color:<br?/>??????????<%=?Html.RadioButton("favColor",?"Blue",?true)?%>?Blue?<br?/>??????????<%=?Html.RadioButton("favColor",?"Purple",?false)%>?Purple?<br?/>??????????<%=?Html.RadioButton("favColor",?"Red",?false)%>?Red?<br?/>??????????<%=?Html.RadioButton("favColor",?"Orange",?false)%>?Orange?<br?/>??????????<%=?Html.RadioButton("favColor",?"Yellow",?false)%>?Yellow?<br?/>??????????<%=?Html.RadioButton("favColor",?"Brown",?false)%>?Brown?<br?/>??????????<%=?Html.RadioButton("favColor",?"Green",?false)%>?Green???????????<br?/><br?/>??????????<%=?Html.CheckBox("bookType")?%>?I?read?more?fiction?than?non-fiction.<br?/>??????????<br?/><br?/>??????????My?favorite?pet:?<%=?Html.DropDownList("pets")?%>??????????<br?/><br?/>??????????<input?type="submit"?value="Submit"?/>??????<%?}?%>????</asp:Content>??
如圖填寫表單數據:
分別使用不同的表單處理方法,對提交的表單數據在視圖FormResults呈現。
提交表單對應的HomeController,包含以不同方法獲取表單數據的代碼,如下:
[csharp] view plaincopy
using?System;??using?System.Collections.Generic;??using?System.Linq;??using?System.Web;??using?System.Web.Mvc;????namespace?HtmlHelper.Controllers??{??????[HandleError]??????public?class?HomeController?:?Controller??????{??????????public?ActionResult?Index()??????????{??????????????ViewData["Message"]?=?"歡迎使用?ASP.NET?MVC!";??????????????????????????????List<string>?petList?=?new?List<string>();??????????????petList.Add("Dog");??????????????petList.Add("Cat");??????????????petList.Add("Hamster");??????????????petList.Add("Parrot");??????????????petList.Add("Gold?fish");??????????????petList.Add("Mountain?lion");??????????????petList.Add("Elephant");????????????????ViewData["Pets"]?=?new?SelectList(petList);????????????????return?View();??????????}????????????public?ActionResult?About()??????????{??????????????return?View();??????????}????????????????????????????????????????????????????public?ActionResult?HandleForm()??????????{??????????????ViewData["name"]?=?Request["name"];??????????????ViewData["favColor"]?=?Request["favColor"];??????????????ViewData["bookType"]?=?Request["bookType"];??????????????ViewData["pet"]?=?Request["pets"];????????????????return?View("FormResults");??????????}??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????}??}?? 在FormResults視圖顯示ViewData的數據,如圖所示:
總結
以上是生活随笔為你收集整理的ASP.NET MVC中在Action获取提交的表单数据方法总结的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。