ASP.NET MVC中在Action獲取提交的表單數據方法總結 (4種方法,轉載備忘)

有Index視圖如下:

視圖代碼如下:

?

[html] view plaincopyprint?
  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>??
<%@ 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 plaincopyprint?
  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. }??
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();}/// <summary>/// 處理表單提交數據,方法1:使用傳統的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>/// 處理表單提交數據,方法2:Action參數名與表單元素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>/// 處理表單提交數據,方法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>/// 處理表單提交數據,方法4:使用實體作為Action參數傳入,前提是提交的表單元素名稱與實體屬性名稱一一對應/// </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的數據,如圖所示:

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/386317.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/386317.shtml
英文地址,請注明出處:http://en.pswp.cn/news/386317.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

解析底層原理!Android開發者面試如何系統復習?幫你突破瓶頸

現狀 后端轉 Android 我該從何處下手&#xff0c;現在學習 android 晚嗎&#xff1f; 我的回答是晚還不至于&#xff0c;因為目前是市場趨于穩定正常&#xff0c;這個是市場發展的比如趨勢&#xff0c;現在火爆大家都看好的人工智能&#xff0c;大數據&#xff0c;猶如2010年…

SpringMVC與Struts2區別與比較總結

SpringMVC與Struts2區別與比較總結 1、Struts2是類級別的攔截&#xff0c; 一個類對應一個request上下文&#xff0c;SpringMVC是方法級別的攔截&#xff0c;一個方法對應一個request上下文&#xff0c;而方法同時又跟一個url對應&#xff0c;所以說從架構本身上SpringMVC就容易…

解析底層原理!月薪20k+的Android面試都問些什么?深夜思考

正文 Android行業主要問題是初級Android太多了&#xff0c;會寫xml和Activity的程序員太多了&#xff0c;初中級程序員面臨很大的競爭&#xff0c;現狀也就偏于高級開發者。越來越多的初中級Android程序員找不到滿意的工作&#xff0c;甚至根本找不到工作&#xff01;所以很多…

windows2000 ,windowsXP和windows2003共享頁面文件

為了緩解大型應用程序對系統內存的壓力,windows系統采用了頁面文件(windows2000以后被叫做pagefile.sys,放在系統分區的根目錄下),來存儲內存中暫時不用的數據或程序.從而提高系統的性能.一般應該將頁面文件的最小值設置為物理內存的2倍,最大值也設為最小值的2倍.拿現在主流的5…

解鎖Android性能優化的五大誤區!滿滿干貨指導

籠統來說&#xff0c;中年程序員容易被淘汰的原因其實不外乎三點。 1、輸出能力已到頂點。這個人奮斗十來年了&#xff0c;依舊碌碌無為&#xff0c;很明顯這人的天花板就這樣了&#xff0c;說白了&#xff0c;天賦就這樣。 2、適應能力越來越差。年紀大&#xff0c;有家庭&…

python查看文件的編碼格式

pip install chardet 執行 import chardet f open(a.doc,r) data f.read() print chardet.detect(data) 結果 {confidence: 0.64465744, encoding: utf-8} 前面是相似度 后面是編碼格式 或者 return chardet.detect(data).get("encoding") 直接獲取文件編碼格式 轉…

意外收獲字節跳動內部資料,一篇文章幫你解答

前言 俗話說“生于憂患&#xff0c;死于安樂”&#xff0c;其實大部分中年危機&#xff0c;就是在安樂中產生的。 有的人或許會反駁&#xff0c;“照你這么說&#xff0c;我還必須奮斗了&#xff0c;不奮斗就要死&#xff0c;難道選擇安逸的生活就不對嗎&#xff1f;我就沒有…

成功跳槽百度工資從15K漲到28K,已整理成文檔

開頭 在一般的互聯網公司的技術人員的面試中&#xff0c;大概會經歷3到4輪的面試&#xff0c;差不多2-3輪的技術面&#xff0c;還有1輪的HR面試&#xff0c;有人面試題是有關“目標”&#xff0c;有的關于“方法”&#xff0c;有的關于“算法”&#xff0c;有的關于“基礎”。…

oracle pl/sql 基礎

PL/SQL筆記PL/SQL塊中只能直接嵌入SELECT,DML(INSERT,UPDATE,DELETE)以及事務控制語句(COMMIT,ROLLBACK,SAVEPOINT),而不能直接嵌入DDL語句(CREATE,ALTER,DROP)和DCL語句(GRANT,REVOKE) 1.檢索單行數據 1.1使用標量變量接受數據 v_ename emp.ename%type; v_sal emp.sal%…

我三年開發經驗,從字節跳動抖音離職后,吐血整理

前言 前不久寫過一篇博客&#xff0c;里面介紹了一位朋友由二本渣渣畢業在外包公司工作兩年多后&#xff0c;跳槽逆襲成功&#xff0c;現在進入了OPPO公司的故事。 后面很多朋友私信我&#xff0c;表示想要這位朋友的面經。 其實我覺得&#xff0c;大家對面經完全沒必要這么…

熊逸《唐詩50講》田園篇 - 學習筆記與感想

此篇已加入熊逸《唐詩50講》學習筆記索引目錄。 一、田園篇具體內容 田園牧歌對于在現代社會里打拼的人們來說&#xff0c;距離一萬光年&#xff0c;但是身心俱疲的時候&#xff0c;讀兩首田園詩卻是最好的治愈&#xff0c;因為詩里歲月柔軟、風物沛然。這一篇里&#xff0c;熊…

我三年開發經驗,從字節跳動抖音離職后,滿滿干貨指導

前言 程序員這個行業&#xff0c;日新月異&#xff0c;技術體系更新速度快&#xff0c;新技術新框架層出不窮&#xff0c;所有的技術都像是一個無底洞&#xff0c;當你學得越多就會發現不懂的越多&#xff0c;不懂的越多&#xff0c;需要學習的就更多。 因此&#xff0c;一旦…

jquery $(this)和this

jQuery中this與$(this)的區別 $("#textbox").hover( function() { this.title "Test"; }, fucntion() { this.title "OK”; } ); 這里的this其實是一個Html 元素(textbox)&#xff0c;…

我了解到的面試的一些小內幕!順利通過阿里Android崗面試

前言 從畢業到現在面試也就那么幾家公司&#xff0c;單前幾次都比較順利&#xff0c;在面到第三家時都給到了我offer&#xff01;前面兩次找工作&#xff0c;沒考慮到以后需要什么&#xff0c;自己的對未來的規劃是什么&#xff0c;只要有份工作&#xff0c;工資符合自己的要求…

React-redux框架之connect()與Provider組件 用法講解

react-redux 在react-redux 框架中&#xff0c;給我提供了兩個常用的API來配合Redux框架的使用&#xff0c;其實在我們的實際項目開發中&#xff0c;我們完全可以不用react-redux框架&#xff0c;但是如果使用此框架&#xff0c;就如虎添翼了。 我們來簡單聊聊這兩個常用的API …

我們究竟還要學習哪些Android知識?吐血整理

前言 閑來無事在家偶然翻到了之前整理的文檔和面試要做到準備路線&#xff0c;雖然內容有點多&#xff0c;但是技多不壓身&#xff0c;多多益善 本部分內容是關于Android進階的一些知識總結&#xff0c;涉及到的知識點比較雜&#xff0c;不過都 是面試中幾乎常問的知識點&…

海明距離

處理 非遞減或者非遞增 排列 的時候 &#xff0c;可以使用計數排序&#xff0c;將時間 復雜度變為 O&#xff08;N&#xff09;&#xff0c;空間復雜度變為O&#xff08;1&#xff09;。 1 int heightChecker(vector<int>& heights) {2 vector<int> res(10…

我們究竟還要學習哪些Android知識?滿滿干貨指導

咸魚翻身不斷上演 2018年5月份&#xff0c;北京&#xff0c;在所謂的互聯網寒冬里&#xff0c;一個普通二本的學生&#xff0c;在小公司工作一年后&#xff0c;跳槽拿到了百度的offer&#xff0c;月薪從9k變為17k&#xff0c;漲薪幅度接近100%。 2018年底&#xff0c;上海&am…

ElasticSearch6.3腳本更新

使用上篇文章創建的索引進行學習&#xff1a;https://www.cnblogs.com/wangymd/p/11200996.html 官方文檔&#xff1a;https://www.elastic.co/guide/en/elasticsearch/painless/6.3/painless-examples.html 1、腳本更新指定字段 方式1&#xff1a; POST test_index/test_type…

我們究竟還要學習哪些Android知識?看這一篇就夠了!

雪上加霜 本人一名Android程序員&#xff0c;今年29歲了。大廠小廠都呆過&#xff0c;現在在騰訊工作&#xff01;明明工作順利&#xff0c;家庭和睦兒女成全&#xff0c;但是總是會感覺到&#xff0c;一股無形的壓力&#xff0c;推著我走&#xff01;作為一名程序員我最怕的不…