日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET MVC中在Action获取提交的表单数据方法总结 (4种方法,转载备忘)

發(fā)布時間:2023/11/30 asp.net 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET MVC中在Action获取提交的表单数据方法总结 (4种方法,转载备忘) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

有Index視圖如下:

視圖代碼如下:

?

[html] view plaincopyprint?
  • <%@?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>??
  • <%@ 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>
    如圖填寫表單數(shù)據(jù):

    ?

    分別使用不同的表單處理方法,對提交的表單數(shù)據(jù)在視圖FormResults呈現(xiàn)。

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

    ?

    [csharp] view plaincopyprint?
  • 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!";??
  • ??
  • ????????????//手動構造頁面中下拉框的寵物數(shù)據(jù) ??
  • ????????????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();??
  • ????????}??
  • ??
  • ????????///?<summary> ??
  • ????????///?處理表單提交數(shù)據(jù),方法1:使用傳統(tǒng)的Request請求取值 ??
  • ????????///?</summary> ??
  • ????????///?<returns></returns> ??
  • ????????public?ActionResult?HandleForm()??
  • ????????{??
  • ????????????ViewData["name"]?=?Request["name"];??
  • ????????????ViewData["favColor"]?=?Request["favColor"];??
  • ????????????ViewData["bookType"]?=?Request["bookType"];??
  • ????????????ViewData["pet"]?=?Request["pets"];??
  • ??
  • ????????????return?View("FormResults");??
  • ????????}??
  • ??
  • ????????///?<summary> ??
  • ????????///?處理表單提交數(shù)據(jù),方法2:Action參數(shù)名與表單元素name值一一對應 ??
  • ????????///?</summary> ??
  • ????????///?<param?name="name"></param> ??
  • ????????///?<param?name="favColor"></param> ??
  • ????????///?<param?name="bookType"></param> ??
  • ????????///?<param?name="pets"></param> ??
  • ????????///?<returns></returns> ??
  • ????????//public?ActionResult?HandleForm(string?name,?string?favColor,?Boolean?bookType,?string?pets) ??
  • ????????//{ ??
  • ????????//????ViewData["name"]?=?name; ??
  • ????????//????ViewData["favColor"]?=?favColor; ??
  • ????????//????ViewData["bookType"]?=?bookType; ??
  • ????????//????ViewData["pet"]?=?pets; ??
  • ??
  • ????????//????return?View("FormResults"); ??
  • ????????//} ??
  • ??
  • ????????///?<summary> ??
  • ????????///?處理表單提交數(shù)據(jù),方法3:從MVC封裝的FormCollection容器中讀取 ??
  • ????????///?</summary> ??
  • ????????///?<param?name="form"></param> ??
  • ????????///?<returns></returns> ??
  • ????????//public?ActionResult?HandleForm(FormCollection?form) ??
  • ????????//{ ??
  • ????????//????ViewData["name"]?=?form["name"]; ??
  • ????????//????ViewData["favColor"]?=?form["favColor"]; ??
  • ????????//????ViewData["bookType"]?=?form["bookType"]; ??
  • ????????//????ViewData["pet"]?=?form["pets"]; ??
  • ??
  • ????????//????return?View("FormResults"); ??
  • ????????//} ??
  • ??
  • ????????///?<summary> ??
  • ????????///?處理表單提交數(shù)據(jù),方法4:使用實體作為Action參數(shù)傳入,前提是提交的表單元素名稱與實體屬性名稱一一對應 ??
  • ????????///?</summary> ??
  • ????????///?<param?name="request"></param> ??
  • ????????///?<returns></returns> ??
  • ????????//[HttpPost] ??
  • ????????//public?ActionResult?HandleForm(InforModel?infor) ??
  • ????????//{ ??
  • ????????//????ViewData["name"]?=?infor.name; ??
  • ????????//????ViewData["favColor"]?=?infor.favColor; ??
  • ????????//????ViewData["bookType"]?=?infor.bookType; ??
  • ????????//????ViewData["pet"]?=?infor.pets; ??
  • ??
  • ????????//????return?View("FormResults"); ??
  • ????????//} ??
  • ??
  • ????}??
  • }??
  • 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!";//手動構造頁面中下拉框的寵物數(shù)據(jù)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();}/// <summary>/// 處理表單提交數(shù)據(jù),方法1:使用傳統(tǒng)的Request請求取值/// </summary>/// <returns></returns>public ActionResult HandleForm(){ViewData["name"] = Request["name"];ViewData["favColor"] = Request["favColor"];ViewData["bookType"] = Request["bookType"];ViewData["pet"] = Request["pets"];return View("FormResults");}/// <summary>/// 處理表單提交數(shù)據(jù),方法2:Action參數(shù)名與表單元素name值一一對應/// </summary>/// <param name="name"></param>/// <param name="favColor"></param>/// <param name="bookType"></param>/// <param name="pets"></param>/// <returns></returns>//public ActionResult HandleForm(string name, string favColor, Boolean bookType, string pets)//{// ViewData["name"] = name;// ViewData["favColor"] = favColor;// ViewData["bookType"] = bookType;// ViewData["pet"] = pets;// return View("FormResults");//}/// <summary>/// 處理表單提交數(shù)據(jù),方法3:從MVC封裝的FormCollection容器中讀取/// </summary>/// <param name="form"></param>/// <returns></returns>//public ActionResult HandleForm(FormCollection form)//{// ViewData["name"] = form["name"];// ViewData["favColor"] = form["favColor"];// ViewData["bookType"] = form["bookType"];// ViewData["pet"] = form["pets"];// return View("FormResults");//}/// <summary>/// 處理表單提交數(shù)據(jù),方法4:使用實體作為Action參數(shù)傳入,前提是提交的表單元素名稱與實體屬性名稱一一對應/// </summary>/// <param name="request"></param>/// <returns></returns>//[HttpPost]//public ActionResult HandleForm(InforModel infor)//{// ViewData["name"] = infor.name;// ViewData["favColor"] = infor.favColor;// ViewData["bookType"] = infor.bookType;// ViewData["pet"] = infor.pets;// return View("FormResults");//}} }

    在FormResults視圖顯示ViewData的數(shù)據(jù),如圖所示:

    轉載于:https://www.cnblogs.com/CielWater/p/3282133.html

    總結

    以上是生活随笔為你收集整理的ASP.NET MVC中在Action获取提交的表单数据方法总结 (4种方法,转载备忘)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。