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

视图代码如下:
- <%@ 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>
如图填写表单数据:

分别使用不同的表单处理方法,对提交的表单数据在视图FormResults呈现。
提交表单对应的HomeController,包含以不同方法获取表单数据的代码,如下:
- 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");
- //}
- }
- }
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种方法,转载备忘)的更多相关文章
- ASP.NET MVC中在Action获取提交的表单数据方法
有Index视图如下: 视图代码如下: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Mas ...
- Asp.net Mvc中分部视图获取后台数据并展示
方式一: 1.主页面中代码: @{Html.RenderAction("CreateLeftMenu");} 2.Controller中代码: public PartialView ...
- 在ASP.NET MVC中使用UEditor无法提交的解决办法
很简单的一个ajax提交,却怎么都不成功 $.ajax({ type: "POST", url: "/mms/riskmanage/commitreply", ...
- 如果asp.net mvc中某个action被执行了两次,请检查是不是以下的原因
注释 <link rel="icon" href="#"> 这一句后试试
- Spring Mvc 前台数据的获取、SpringMvc 表单数据的获取
首先在web.xml 里面配置一个编码过滤器 <!-- springmvc框架本身没有处理请求编码,我们自己配置一个请求编码过滤器 --> <filter> <filte ...
- 在Asp.Net MVC中使用ModelBinding构造Array、List、Collection以及Dictionary
在asp.net mvc中,我们可以在html表单中使用特定的格式传递参数,从而通过model binder构造一些集合类型. 第一种方式 public ActionResult Infancy(Pe ...
- asp.net MVC中使用Html.Checkbox提示该字符串未被识别为有效的布尔值错误的解决方法
在asp.net MVC中使用Html.CheckBox提交后出现该字符串未被识别为有效的布尔值错误,或从类型“System.String”到类型“System.Boolean”的参数转换失败. 错误 ...
- 在ASP.NET MVC中实现Select多选
我们知道,在ASP.NET MVC中实现多选Select的话,使用Html.ListBoxFor或Html.ListBox方法就可以.在实际应用中,到底该如何设计View Model, 控制器如何接收 ...
- 在ASP.NET MVC 中获取当前URL、controller、action 、参数
URL的获取很简单,ASP.NET通用:[1]获取 完整url (协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [2]获取 虚拟目录名 ...
随机推荐
- 在WebBrowser中通过模拟键盘鼠标操控网页中的文件上传控件(转)
引言 这两天沉迷了Google SketchUp,刚刚玩够,一时兴起,研究了一下WebBrowser. 我在<WebBrowser控件使用技巧分享>一文中曾谈到过“我现在可以通过WebBr ...
- jQuery插件开发--(转)
1,开始 可以通过为jQuery.fn增加一个新的函数来编写jQuery插件.属性的名字就是你的插件的名字: jQuery.fn.myPlugin = function(){ //开始写你的代码吧! ...
- css样式—字体垂直、水平居中
“来,老板娘,给个div瞅瞅”: “好的,宇哥,来了了了”: <div class="tt">啦啦啦</div> “给各样啊,我去”: “是”: .tt{ ...
- Android学习系列(37)--App调试内存泄露之Context篇(下)
接着<Android学习系列(36)--App调试内存泄露之Context篇(上)>继续分析. 5. AsyncTask对象 我N年前去盛大面过一次试,当时面试官极力推荐我使用AsyncT ...
- Uva 699The Falling Leaves
0.唔.这道题 首先要明确根节点在哪儿 初始化成pos=maxn/2; 1.因为是先序的输入方法,所以这个建树的方法很重要 void build(int p) { int v; cin>> ...
- yii2.0 的数据的 增
增加数据 /** * 添加数据 controller 层 */ //引入对应的model类 use app\models\About; //定义对应的方法固定的actionxxxx ...
- MFC MSBDutyTable下载地址
点击此处跳转到下载地址 简明教程: 对于非制表人,只需要添加空余时间-新建,然后点星期和节数有课的那个按钮,勾选自己有课的周数.全部勾好后,生成空余时间表.然后查看自己的空余时间表,并导出,发给制表人 ...
- ora-00031:session marked for kill处理oracle中杀不掉的锁
http://www.cnblogs.com/songdavid/articles/2223869.html 一些ORACLE中的进程被杀掉后,状态被置为"killed",但是锁定 ...
- BZOJ4428 : [Nwerc2015]Debugging调试
设$f[i]$为最优策略下调试$i$行代码的时间,则: $f[1]=0$ $f[i]=\min((j-1)\times p+f[\lceil\frac{i}{j}\rceil])+r$ 意义为枚举pr ...
- 在SUBLIME TEXT中安装SUBLIMELINTER进行JS&CSS代码校验
一:Sublime Text 中需要先安装Package Control.(如果有则无需安装) 安装方法:打开Sublime Text控制台(快捷键Ctrl+`),在控制台粘贴以下代码,按回车执行. ...