有Index视图如下:

视图代码如下:

  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>

如图填写表单数据:

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

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

  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. }

在FormResults视图显示ViewData的数据,如图所示:

ASP.NET MVC中在Action获取提交的表单数据方法的更多相关文章

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

    有Index视图如下: 视图代码如下: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Mas ...

  2. 在ASP.NET MVC中使用UEditor无法提交的解决办法

    很简单的一个ajax提交,却怎么都不成功 $.ajax({ type: "POST", url: "/mms/riskmanage/commitreply", ...

  3. Asp.net Mvc中分部视图获取后台数据并展示

    方式一: 1.主页面中代码: @{Html.RenderAction("CreateLeftMenu");} 2.Controller中代码: public PartialView ...

  4. 如果asp.net mvc中某个action被执行了两次,请检查是不是以下的原因

    注释 <link rel="icon" href="#"> 这一句后试试

  5. 解决Asp.net Mvc中使用异步的时候HttpContext.Current为null的方法

    在项目中使用异步(async await)的时候发现一个现象,HttpContext.Current为null,导致一系列的问题. 上网查了一些资料后找到了一个对象: System.Threading ...

  6. Asp.net MVC中文件上传的参数转对象的方法

    参照博友的.NET WebApi上传文件接口(带其他参数)实现文件上传并带参数,当需要多个参数时,不想每次都通过HttpContext.Request.Params去取值,就针对HttpRequest ...

  7. Spring Mvc 前台数据的获取、SpringMvc 表单数据的获取

    首先在web.xml 里面配置一个编码过滤器 <!-- springmvc框架本身没有处理请求编码,我们自己配置一个请求编码过滤器 --> <filter> <filte ...

  8. Asp.net mvc 中的路由

    在 Asp.net mvc 中,来自客户端的请求总是针对某个 Controller 中的 Action 方法,因此,必须采用某种机制从请求的 URl 中解析出对应的 Controller 和 Acti ...

  9. 在Asp.Net MVC中使用ModelBinding构造Array、List、Collection以及Dictionary

    在asp.net mvc中,我们可以在html表单中使用特定的格式传递参数,从而通过model binder构造一些集合类型. 第一种方式 public ActionResult Infancy(Pe ...

随机推荐

  1. Android 使用GPS获取到经纬度后 无法在Android8.0上使用Geocoder类获取位置信息

    由于我的应用在获取到经纬度后在Android8.0不能使用如下代码获取位置信息.只好使用百度地图 WEB服务API 通过调接口的方式获取位置信息. Geocoder geocoder = new Ge ...

  2. Storm实现实时大数据分析

    当今世界,公司的日常运营经常会生成TB级别的数据.数据来源囊括了互联网装置可以捕获的任何类型数据,网站.社交媒体.交易型商业数据以及其它商业环境中创建的数据.考虑到数据的生成量,实时处理成为了许多机构 ...

  3. react-music React全家桶项目,精品之作!

    React-Music 全家桶项目,精品之作! 一.简介 该项目是基于React全家桶开发的一个音乐播放器,技术栈采用:Webpack + React + React-redux + React-ro ...

  4. 多年前写的DataTable与实体类的转换

    介绍 介绍 很多年前一直使用Ado.net,后来慢慢转型到其他的orm,在转型过程中,有意向将两者的模型结合起来,利用DataTable中的行状态完善一些mvc中的数据控制作用.现在把它放出来,留个纪 ...

  5. iOS AES128 CBC No Padding加密解密

    最近的项目中数据传输用到加密,项目选择了AES128 CBC No Padding加密方式,PHP和Android方面的代码网上太多了.但是唯独没有iOS的,但是也有别的写法,但不是是AES128 C ...

  6. 用CSS3写圆角(超简单)

    前缀: -moz(例如 -moz-border-radius)用于Firefox-webkit(例如:-webkit-border-radius)用于Safari和Chrome. CSS3圆角(所有的 ...

  7. 2016.5.24——Intersection of Two Linked Lists

    Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...

  8. 如何调整Linux内核启动中的驱动初始化顺序-驱动加载优先级

    Linux内核为不同驱动的加载顺序对应不同的优先级,定义了一些宏: include\linux\init.h #define pure_initcall(fn) __define_initcall(& ...

  9. plupload 上传组件的使用

    在这之前在感谢园子好多大牛的文章,在这里就不列出来了. 进入正题. svn检索https://github.com/moxiecode/plupload 获取到代码,这篇文章使用的是v2.1.8 主要 ...

  10. Pytorch数据变换(Transform)

    实例化数据库的时候,有一个可选的参数可以对数据进行转换,满足大多神经网络的要求输入固定尺寸的图片,因此要对原图进行Rescale或者Crop操作,然后返回的数据需要转换成Tensor如: import ...