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 ...
随机推荐
- CF&&CC百套计划2 CodeChef December Challenge 2017 Total Diamonds
https://www.codechef.com/DEC17/problems/VK18 #include<cstdio> #include<iostream> #includ ...
- noi题库(noi.openjudge.cn) 1.11编程基础之二分查找T01、02、04
T01 查找最接近的元素 描述 在一个非降序列中,查找与给定值最接近的元素. 输入 第一行包含一个整数n,为非降序列长度.1 <= n <= 100000.第二行包含n个整数,为非降序列各 ...
- RTSP服务器之————rtsp-server(轻量级RTSP / RTP流媒体服务器)
github:https://github.com/revmischa/rtsp-server 轻量级RTSP / RTP流媒体服务器
- Rolling in the Deep (Learning)
Rolling in the Deep (Learning) Deep Learning has been getting a lot of press lately, and is one of t ...
- 20155202 2016-2017-2 《Java程序设计》第7周学习总结
20155202 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 世界协调时间:UTC 采用 闰秒修正 Epoch为某特定时代开始,时间轴上某一瞬间 Unix ...
- 配置ODBC DSN数据源,导出数据库数据到Excel过程记录
一.前言 工作中我们可能遇到这样的需要:查询数据库中的信息,并将结果导出到Excel文件.这本来没什么,但数据量比较大时,用PLSQL.toad导出Excel会出现内存不足等情况,使用odbc+Mic ...
- Djangoform表单Ajax控制跳转
需求: 1:在登陆页面输入账号密码后,ajax异步提交数据给后端验证. 2:验证通过后,后端指定跳转页面,并把页面封装进返回的Json数据中,由ajax控制from表单跳转到目标页面 一:登陆页面HT ...
- 【译】第九篇 Integration Services:控制流任务错误
本篇文章是Integration Services系列的第九篇,详细内容请参考原文. 简介在前面三篇文章,我们创建了一个新的SSIS包,学习了脚本任务和优先约束,并检查包的MaxConcurrentE ...
- 2016.6.21——Add Binary
Add Binary 本题收获: 对于相加而言,考虑进位以及进位之后的值为多少,全部进位完毕后进位还为1(11 + 11 = 110)需要添加一位.1.string中默认每个元素为char型 2.从i ...
- 【codeforces】【比赛题解】#940 CF Round #466 (Div. 2)
人生的大起大落莫过如此,下一场我一定要回紫. [A]Points on the line 题意: 一个直线上有\(n\)个点,要求去掉最少的点,使得最远两点距离不超过\(d\). 题解: 暴力两重fo ...