ASP.NET MVC中在Action获取提交的表单数据方法
有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>
如图填写表单数据:
分别使用不同的表单处理方法,对提交的表单数据在视图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");
- //}
- }
- }
在FormResults视图显示ViewData的数据,如图所示:
ASP.NET MVC中在Action获取提交的表单数据方法的更多相关文章
- ASP.NET MVC中在Action获取提交的表单数据方法总结 (4种方法,转载备忘)
有Index视图如下: 视图代码如下: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Mas ...
- 在ASP.NET MVC中使用UEditor无法提交的解决办法
很简单的一个ajax提交,却怎么都不成功 $.ajax({ type: "POST", url: "/mms/riskmanage/commitreply", ...
- Asp.net Mvc中分部视图获取后台数据并展示
方式一: 1.主页面中代码: @{Html.RenderAction("CreateLeftMenu");} 2.Controller中代码: public PartialView ...
- 如果asp.net mvc中某个action被执行了两次,请检查是不是以下的原因
注释 <link rel="icon" href="#"> 这一句后试试
- 解决Asp.net Mvc中使用异步的时候HttpContext.Current为null的方法
在项目中使用异步(async await)的时候发现一个现象,HttpContext.Current为null,导致一系列的问题. 上网查了一些资料后找到了一个对象: System.Threading ...
- Asp.net MVC中文件上传的参数转对象的方法
参照博友的.NET WebApi上传文件接口(带其他参数)实现文件上传并带参数,当需要多个参数时,不想每次都通过HttpContext.Request.Params去取值,就针对HttpRequest ...
- Spring Mvc 前台数据的获取、SpringMvc 表单数据的获取
首先在web.xml 里面配置一个编码过滤器 <!-- springmvc框架本身没有处理请求编码,我们自己配置一个请求编码过滤器 --> <filter> <filte ...
- Asp.net mvc 中的路由
在 Asp.net mvc 中,来自客户端的请求总是针对某个 Controller 中的 Action 方法,因此,必须采用某种机制从请求的 URl 中解析出对应的 Controller 和 Acti ...
- 在Asp.Net MVC中使用ModelBinding构造Array、List、Collection以及Dictionary
在asp.net mvc中,我们可以在html表单中使用特定的格式传递参数,从而通过model binder构造一些集合类型. 第一种方式 public ActionResult Infancy(Pe ...
随机推荐
- myeclipse使用小结
1.项目设置编码格式 (1)全局编码设置:编码设置的方法:ToolBar-->Window-->Preferences-->General-->Workspace-->T ...
- Redis学习四:解析配置文件 redis.conf
一.它在哪 地址: 思考:为什么要将它拷贝出来单独执行? 二.Units单位 1 配置大小单位,开头定义了一些基本的度量单位,只支持bytes,不支持bit 2 对大小写不敏感 三.INCLUDES包 ...
- Java并发编程原理与实战二:并行&并发&多线程的理解
1.CPU的发展趋势: 核心数目依旧会越来越多,根据摩尔定律,由于单个核心性能提升有着严重的瓶颈问题,普通的PC桌面在2018年可能回到24核心. 2.并发和并行的区别: 所有的并发处理都有排队等候, ...
- java后台代码发送邮件
1:安装 eyoumailserversetup 易邮邮件服务器 注册账号 2:安装Foxmail 登录以后会有个还原页面 3:测试 4:java 代码编写 配置文件: mail.host=http ...
- Eclipse改变相同代码高亮颜色
一.点击某一代码时,让相同代码高亮显示(Eclipse默认是这样的) Window ->preferences ->Java ->Editor ->Mark Occurrenc ...
- MySQL Sakila样本数据库
Sakila样本数据库介绍 Sakila样本数据库是MySQL官方提供的一个模拟DVD租赁信息管理的数据库,提供了一个标准模式,可作为书中例子,教程.文章.样品,等等,对学习测试来说是个不错的选择. ...
- ECMAScript 6中的var,let,const
var的变量提升 console.log(a); //输出undefined ; 他的实际执行顺序是: var a: console.log(a); a= 这就是var的变量提升 const命令的用法 ...
- Dream------scala--类的属性和对象私有字段实战详解
Scala类的属性和对象私有字段实战详解 一.类的属性 scala类的属性跟java有比较大的不同,需要注意的是对象的私有(private)字段 1.私有字段:字段必须初始化(当然即使不是私有字段也要 ...
- 三、springcloud之服务调用Feign
一.背景 项目中接口调用: Httpclient Okhttp Httpurlconnection RestTemplate 微服务提供了更简单,方便的Feign 二.Feign简介 Feign是一个 ...
- 关于vc++ 6.0 编译器,点打开文件时自动关闭
装好VC++ 6.0后,点打开文件时编译器会自动关闭掉,然后在网上找到各位大神写的资料,果然是因为之前有安装vs2010冲突的缘故,然后http://download.csdn.net/source/ ...