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

有Index视图如下:

ASP.NET MVC中在Action获取提交的表单数据方法总结 (4种方法,转载备忘)文章地址https://www.yii666.com/article/764160.html网址:yii666.com

视图代码如下:

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

如图填写表单数据:

ASP.NET MVC中在Action获取提交的表单数据方法总结 (4种方法,转载备忘)文章来源地址:https://www.yii666.com/article/764160.html

分别使用不同的表单处理方法,对提交的表单数据在视图FormResults呈现。

提交表单对应的HomeController,包含以不同方法获取表单数据的代码,如下:网址:yii666.com<

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace HtmlHelper.Controllers
  7. {
  8. [HandleError]
  9. public class HomeController : Controller
  10. {
  11. public ActionResult Index()
  12. {
  13. ViewData["Message"] = "欢迎使用 ASP.NET MVC!";
  14. //手动构造页面中下拉框的宠物数据
  15. List<string> petList = new List<string>();
  16. petList.Add("Dog");
  17. petList.Add("Cat");
  18. petList.Add("Hamster");
  19. petList.Add("Parrot");
  20. petList.Add("Gold fish");
  21. petList.Add("Mountain lion");
  22. petList.Add("Elephant");
  23. ViewData["Pets"] = new SelectList(petList);
  24. return View();
  25. }
  26. public ActionResult About()
  27. {
  28. return View();
  29. }
  30. /// <summary>
  31. /// 处理表单提交数据,方法1:使用传统的Request请求取值
  32. /// </summary>
  33. /// <returns></returns>
  34. public ActionResult HandleForm()
  35. {
  36. ViewData["name"] = Request["name"];
  37. ViewData["favColor"] = Request["favColor"];
  38. ViewData["bookType"] = Request["bookType"];
  39. ViewData["pet"] = Request["pets"];
  40. return View("FormResults");
  41. }
  42. /// <summary>
  43. /// 处理表单提交数据,方法2:Action参数名与表单元素name值一一对应
  44. /// </summary>
  45. /// <param name="name"></param>
  46. /// <param name="favColor"></param>
  47. /// <param name="bookType"></param>
  48. /// <param name="pets"></param>
  49. /// <returns></returns>
  50. //public ActionResult HandleForm(string name, string favColor, Boolean bookType, string pets)
  51. //{
  52. //    ViewData["name"] = name;
  53. //    ViewData["favColor"] = favColor;
  54. //    ViewData["bookType"] = bookType;
  55. //    ViewData["pet"] = pets;
  56. //    return View("FormResults");
  57. //}
  58. /// <summary>
  59. /// 处理表单提交数据,方法3:从MVC封装的FormCollection容器中读取
  60. /// </summary>
  61. /// <param name="form"></param>
  62. /// <returns></returns>
  63. //public ActionResult HandleForm(FormCollection form)
  64. //{
  65. //    ViewData["name"] = form["name"];
  66. //    ViewData["favColor"] = form["favColor"];
  67. //    ViewData["bookType"] = form["bookType"];
  68. //    ViewData["pet"] = form["pets"];
  69. //    return View("FormResults");
  70. //}
  71. /// <summary>
  72. /// 处理表单提交数据,方法4:使用实体作为Action参数传入,前提是提交的表单元素名称与实体属性名称一一对应
  73. /// </summary>
  74. /// <param name="request"></param>
  75. /// <returns></returns>
  76. //[HttpPost]
  77. //public ActionResult HandleForm(InforModel infor)
  78. //{
  79. //    ViewData["name"] = infor.name;
  80. //    ViewData["favColor"] = infor.favColor;
  81. //    ViewData["bookType"] = infor.bookType;
  82. //    ViewData["pet"] = infor.pets;
  83. //    return View("FormResults");
  84. //}
  85. }
  86. }
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的数据,如图所示:

ASP.NET MVC中在Action获取提交的表单数据方法总结 (4种方法,转载备忘)文章来源地址https://www.yii666.com/article/764160.html

版权声明:本文内容来源于网络,版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。文本页已经标记具体来源原文地址,请点击原文查看来源网址,站内文章以及资源内容站长不承诺其正确性,如侵犯了您的权益,请联系站长如有侵权请联系站长,将立刻删除

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

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

  2. JSON编码格式提交表单数据详解

  3. easyui提交表单数据的时候如何防止二次提交

  4. strus2中获取表单数据 两种方式 属性驱动 和模型驱动

  5. 使用WebClient上传文件并同时Post表单数据字段到服务端

  6. 不使用BeanUtils,利用Java反射机制:表单数据自动封装到JavaBean

  7. 初学者易上手的SSH-struts2 02Action获取表单数据-通配符

    在上一章中,我们已经搭建好了struts2的一个开发环境,那么这一章就来做一个简单的登录功能,以及介绍和使用struts2里面一个重要的东西-通配符。第一步,在WebContent下面新建一个login.jsp的页面,里面使用form表单实现一个简单的登录页面。第二步:打开上一章中建好的LoginAc

  8. Spring MVC Ajax 嵌套表单数据的提交

    概述在一些场景里,某个大表单里常常嵌套着一个或若干个小逻辑块,比如以下表单里“设计预审”中包括了一个子模块表单“拟定款项”。在这种情况下该怎么去设计实体类以及表单呢?实体类的设计在设计实体类时最好的方式是“主模块包括了自己的字段,而子模块只通

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信图片_20190322181744_03.jpg

微信扫一扫打赏

请作者喝杯咖啡吧~

支付宝扫一扫领取红包,优惠每天领

二维码1

zhifubaohongbao.png

二维码2

zhifubaohongbao2.png