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的数据,如图所示:
转载于:https://www.cnblogs.com/CielWater/p/3282133.html
ASP.NET MVC中在Action获取提交的表单数据方法总结 (4种方法,转载备忘)相关推荐
- ASP.NET MVC中在Action获取提交的表单数据方法总结
有Index视图如下: 视图代码如下: [html] view plaincopy <%@ Page Language="C#" MasterPageFile="~ ...
- Asp.net MVC中防止HttpPost重复提交
重复提交的场景很常见,可能是当时服务器延迟的原因,如购物车物品叠加,重复提交多个订单.常见的解决方法是提交后把Button在客户端Js禁用,或是用Js禁止后退键等.在ASP.NET MVC 3 Web ...
- yii2 html form,YII2中ajax通过post提交form表单数据报400错误的解决方法
摘要:YII2中通过ajax post表单数据需要验证CSRF否则post数据是无法提交过去的.虽然有其他人提供过解决方案,但都不够完整,除了把enableCsrfValidation设为false外 ...
- 如果asp.net mvc中某个action被执行了两次,请检查是不是以下的原因
注释 <link rel="icon" href="#"> 这一句后试试 转载于:https://www.cnblogs.com/lummon/p/ ...
- 在DWR中实现直接获取一个JAVA类的返回值的两种方法
第一种实现(来源网上转贴): js 代码 function Test() { var _data = ""; this.getString = function() { //设置成 ...
- 在Asp.Net MVC中使用ModelBinding构造Array、List、Collection以及Dictionary
在asp.net mvc中,我们可以在html表单中使用特定的格式传递参数,从而通过model binder构造一些集合类型. 第一种方式 public ActionResult Infancy(Pe ...
- 在ASP.NET MVC中实现Select多选
我们知道,在ASP.NET MVC中实现多选Select的话,使用Html.ListBoxFor或Html.ListBox方法就可以.在实际应用中,到底该如何设计View Model, 控制器如何接收 ...
- ajax提交form表单到php,ajax如何提交form表单数据?ajax提交form表单数据的方法介绍...
对于form表单数据的提交,我们一般都会想到使用ajax提交,那么,ajax如何来提交form表单数据呢?接下来的这篇文章就来给大家来介绍关于ajax提交form表单数据方法,有需要的伙伴可以参考一下 ...
- 在ASP.NET MVC 中获取当前URL、controller、action
在ASP.NET MVC 中获取当前URL.controller.action URL的获取很简单,ASP.NET通用: [1]获取 完整url (协议名+域名+虚拟目录名+文件名+参数) stri ...
最新文章
- 写一个函数,2 个参数,1 个字符串,1 个字节数,返回截取的字符串,要求字符串中的中文不能出现乱码
- css多行超出显示点_CSS实现单行、多行文本溢出显示省略号(…)
- Dos 中实现else if 功能
- Hive优化(再一遍系统复习)
- 【CodeForces - 632B】Alice, Bob, Two Teams (预处理,思维,前缀和后缀和)
- JavaScript获取距离某天前或后的日期
- 万万没 想到,Redis性能测试还能这样做
- synchronized锁升级过程详解
- ACC - 简介与解码
- outlook安装包下载
- Java疯狂讲义读书笔记第十章
- 智能优化算法——布谷鸟搜索算法原理(附代码)
- 用svn上的文件,覆盖本地文件
- JumpServer页面访问502、504问题记录
- cnn图像风格转换原理论文整理(一)
- 宝塔+青龙面板+机器人+诺兰
- python离线环境迁移_Python离线项目迁移部署
- 轻松高效搭建可视化数据网站
- 集结社区TOP博主,云享专家优质电子书
- Charles常用抓包用具安装及使用
热门文章
- 您可能不知道的 C++ 关键字
- WCF中的方法重载 实现
- WCF学习(五)数据契约之已知类型
- 绝地求生 android版支持蓝牙吗,《绝地求生》吃鸡必须要顶配吗?这些配置也能畅玩...
- c语言编码风格,讲嵌入式C语言编码风格.ppt
- 算法—回溯法桥本分数式
- C学习杂记(六)%2.0f打印输出宽度
- 公钥和私钥 java_公钥与私钥 - yxhxj2006 - BlogJava
- L8ER的完整形式是什么?
- c# 声明类的时候初始化类_使用C#初始化的列表声明