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

有Index视图如下:

视图代码如下:

 

[html] view plaincopyprint?
  1. <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>  
  2.   
  3. <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">  
  4.     主页  
  5. </asp:Content>  
  6.   
  7. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">  
  8.   
  9.     <h2><%= Html.Encode(ViewData["Message"]) %></h2>  
  10.     <br />  
  11.     <br />  
  12.   
  13.     <% using(Html.BeginForm("HandleForm", "Home")) %>  
  14.     <% { %>  
  15.         Enter your name: <%= Html.TextBox("name") %>  
  16.         <br /><br />  
  17.         Select your favorite color:<br />  
  18.         <%= Html.RadioButton("favColor", "Blue", true) %> Blue <br />  
  19.         <%= Html.RadioButton("favColor", "Purple", false)%> Purple <br />  
  20.         <%= Html.RadioButton("favColor", "Red", false)%> Red <br />  
  21.         <%= Html.RadioButton("favColor", "Orange", false)%> Orange <br />  
  22.         <%= Html.RadioButton("favColor", "Yellow", false)%> Yellow <br />  
  23.         <%= Html.RadioButton("favColor", "Brown", false)%> Brown <br />  
  24.         <%= Html.RadioButton("favColor", "Green", false)%> Green   
  25.         <br /><br />  
  26.         <%= Html.CheckBox("bookType") %> I read more fiction than non-fiction.<br />  
  27.         <br /><br />  
  28.         My favorite pet: <%= Html.DropDownList("pets") %>  
  29.         <br /><br />  
  30.         <input type="submit" value="Submit" />  
  31.     <% } %>  
  32.   
  33. </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,包含以不同方法获取表单数据的代码,如下:

 

[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace HtmlHelper.Controllers  
  8. {  
  9.     [HandleError]  
  10.     public class HomeController : Controller  
  11.     {  
  12.         public ActionResult Index()  
  13.         {  
  14.             ViewData["Message"] = "欢迎使用 ASP.NET MVC!";  
  15.   
  16.             //手动构造页面中下拉框的宠物数据   
  17.             List<string> petList = new List<string>();  
  18.             petList.Add("Dog");  
  19.             petList.Add("Cat");  
  20.             petList.Add("Hamster");  
  21.             petList.Add("Parrot");  
  22.             petList.Add("Gold fish");  
  23.             petList.Add("Mountain lion");  
  24.             petList.Add("Elephant");  
  25.   
  26.             ViewData["Pets"] = new SelectList(petList);  
  27.   
  28.             return View();  
  29.         }  
  30.   
  31.         public ActionResult About()  
  32.         {  
  33.             return View();  
  34.         }  
  35.   
  36.         /// <summary>   
  37.         /// 处理表单提交数据,方法1:使用传统的Request请求取值   
  38.         /// </summary>   
  39.         /// <returns></returns>   
  40.         public ActionResult HandleForm()  
  41.         {  
  42.             ViewData["name"] = Request["name"];  
  43.             ViewData["favColor"] = Request["favColor"];  
  44.             ViewData["bookType"] = Request["bookType"];  
  45.             ViewData["pet"] = Request["pets"];  
  46.   
  47.             return View("FormResults");  
  48.         }  
  49.   
  50.         /// <summary>   
  51.         /// 处理表单提交数据,方法2:Action参数名与表单元素name值一一对应   
  52.         /// </summary>   
  53.         /// <param name="name"></param>   
  54.         /// <param name="favColor"></param>   
  55.         /// <param name="bookType"></param>   
  56.         /// <param name="pets"></param>   
  57.         /// <returns></returns>   
  58.         //public ActionResult HandleForm(string name, string favColor, Boolean bookType, string pets)   
  59.         //{   
  60.         //    ViewData["name"] = name;   
  61.         //    ViewData["favColor"] = favColor;   
  62.         //    ViewData["bookType"] = bookType;   
  63.         //    ViewData["pet"] = pets;   
  64.   
  65.         //    return View("FormResults");   
  66.         //}   
  67.   
  68.         /// <summary>   
  69.         /// 处理表单提交数据,方法3:从MVC封装的FormCollection容器中读取   
  70.         /// </summary>   
  71.         /// <param name="form"></param>   
  72.         /// <returns></returns>   
  73.         //public ActionResult HandleForm(FormCollection form)   
  74.         //{   
  75.         //    ViewData["name"] = form["name"];   
  76.         //    ViewData["favColor"] = form["favColor"];   
  77.         //    ViewData["bookType"] = form["bookType"];   
  78.         //    ViewData["pet"] = form["pets"];   
  79.   
  80.         //    return View("FormResults");   
  81.         //}   
  82.   
  83.         /// <summary>   
  84.         /// 处理表单提交数据,方法4:使用实体作为Action参数传入,前提是提交的表单元素名称与实体属性名称一一对应   
  85.         /// </summary>   
  86.         /// <param name="request"></param>   
  87.         /// <returns></returns>   
  88.         //[HttpPost]   
  89.         //public ActionResult HandleForm(InforModel infor)   
  90.         //{   
  91.         //    ViewData["name"] = infor.name;   
  92.         //    ViewData["favColor"] = infor.favColor;   
  93.         //    ViewData["bookType"] = infor.bookType;   
  94.         //    ViewData["pet"] = infor.pets;   
  95.   
  96.         //    return View("FormResults");   
  97.         //}   
  98.   
  99.     }  
  100. }  
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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/386317.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

解析底层原理!Android开发者面试如何系统复习?帮你突破瓶颈

现状 后端转 Android 我该从何处下手&#xff0c;现在学习 android 晚吗&#xff1f; 我的回答是晚还不至于&#xff0c;因为目前是市场趋于稳定正常&#xff0c;这个是市场发展的比如趋势&#xff0c;现在火爆大家都看好的人工智能&#xff0c;大数据&#xff0c;犹如2010年…

SpringMVC与Struts2区别与比较总结

SpringMVC与Struts2区别与比较总结 1、Struts2是类级别的拦截&#xff0c; 一个类对应一个request上下文&#xff0c;SpringMVC是方法级别的拦截&#xff0c;一个方法对应一个request上下文&#xff0c;而方法同时又跟一个url对应&#xff0c;所以说从架构本身上SpringMVC就容易…

解析底层原理!月薪20k+的Android面试都问些什么?深夜思考

正文 Android行业主要问题是初级Android太多了&#xff0c;会写xml和Activity的程序员太多了&#xff0c;初中级程序员面临很大的竞争&#xff0c;现状也就偏于高级开发者。越来越多的初中级Android程序员找不到满意的工作&#xff0c;甚至根本找不到工作&#xff01;所以很多…

windows2000 ,windowsXP和windows2003共享页面文件

为了缓解大型应用程序对系统内存的压力,windows系统采用了页面文件(windows2000以后被叫做pagefile.sys,放在系统分区的根目录下),来存储内存中暂时不用的数据或程序.从而提高系统的性能.一般应该将页面文件的最小值设置为物理内存的2倍,最大值也设为最小值的2倍.拿现在主流的5…

解锁Android性能优化的五大误区!满满干货指导

笼统来说&#xff0c;中年程序员容易被淘汰的原因其实不外乎三点。 1、输出能力已到顶点。这个人奋斗十来年了&#xff0c;依旧碌碌无为&#xff0c;很明显这人的天花板就这样了&#xff0c;说白了&#xff0c;天赋就这样。 2、适应能力越来越差。年纪大&#xff0c;有家庭&…

python查看文件的编码格式

pip install chardet 执行 import chardet f open(a.doc,r) data f.read() print chardet.detect(data) 结果 {confidence: 0.64465744, encoding: utf-8} 前面是相似度 后面是编码格式 或者 return chardet.detect(data).get("encoding") 直接获取文件编码格式 转…

意外收获字节跳动内部资料,一篇文章帮你解答

前言 俗话说“生于忧患&#xff0c;死于安乐”&#xff0c;其实大部分中年危机&#xff0c;就是在安乐中产生的。 有的人或许会反驳&#xff0c;“照你这么说&#xff0c;我还必须奋斗了&#xff0c;不奋斗就要死&#xff0c;难道选择安逸的生活就不对吗&#xff1f;我就没有…

成功跳槽百度工资从15K涨到28K,已整理成文档

开头 在一般的互联网公司的技术人员的面试中&#xff0c;大概会经历3到4轮的面试&#xff0c;差不多2-3轮的技术面&#xff0c;还有1轮的HR面试&#xff0c;有人面试题是有关“目标”&#xff0c;有的关于“方法”&#xff0c;有的关于“算法”&#xff0c;有的关于“基础”。…

oracle pl/sql 基础

PL/SQL笔记PL/SQL块中只能直接嵌入SELECT,DML(INSERT,UPDATE,DELETE)以及事务控制语句(COMMIT,ROLLBACK,SAVEPOINT),而不能直接嵌入DDL语句(CREATE,ALTER,DROP)和DCL语句(GRANT,REVOKE) 1.检索单行数据 1.1使用标量变量接受数据 v_ename emp.ename%type; v_sal emp.sal%…

我三年开发经验,从字节跳动抖音离职后,吐血整理

前言 前不久写过一篇博客&#xff0c;里面介绍了一位朋友由二本渣渣毕业在外包公司工作两年多后&#xff0c;跳槽逆袭成功&#xff0c;现在进入了OPPO公司的故事。 后面很多朋友私信我&#xff0c;表示想要这位朋友的面经。 其实我觉得&#xff0c;大家对面经完全没必要这么…

熊逸《唐诗50讲》田园篇 - 学习笔记与感想

此篇已加入熊逸《唐诗50讲》学习笔记索引目录。 一、田园篇具体内容 田园牧歌对于在现代社会里打拼的人们来说&#xff0c;距离一万光年&#xff0c;但是身心俱疲的时候&#xff0c;读两首田园诗却是最好的治愈&#xff0c;因为诗里岁月柔软、风物沛然。这一篇里&#xff0c;熊…

我三年开发经验,从字节跳动抖音离职后,满满干货指导

前言 程序员这个行业&#xff0c;日新月异&#xff0c;技术体系更新速度快&#xff0c;新技术新框架层出不穷&#xff0c;所有的技术都像是一个无底洞&#xff0c;当你学得越多就会发现不懂的越多&#xff0c;不懂的越多&#xff0c;需要学习的就更多。 因此&#xff0c;一旦…

jquery $(this)和this

jQuery中this与$(this)的区别 $("#textbox").hover( function() { this.title "Test"; }, fucntion() { this.title "OK”; } ); 这里的this其实是一个Html 元素(textbox)&#xff0c;…

我了解到的面试的一些小内幕!顺利通过阿里Android岗面试

前言 从毕业到现在面试也就那么几家公司&#xff0c;单前几次都比较顺利&#xff0c;在面到第三家时都给到了我offer&#xff01;前面两次找工作&#xff0c;没考虑到以后需要什么&#xff0c;自己的对未来的规划是什么&#xff0c;只要有份工作&#xff0c;工资符合自己的要求…

React-redux框架之connect()与Provider组件 用法讲解

react-redux 在react-redux 框架中&#xff0c;给我提供了两个常用的API来配合Redux框架的使用&#xff0c;其实在我们的实际项目开发中&#xff0c;我们完全可以不用react-redux框架&#xff0c;但是如果使用此框架&#xff0c;就如虎添翼了。 我们来简单聊聊这两个常用的API …

我们究竟还要学习哪些Android知识?吐血整理

前言 闲来无事在家偶然翻到了之前整理的文档和面试要做到准备路线&#xff0c;虽然内容有点多&#xff0c;但是技多不压身&#xff0c;多多益善 本部分内容是关于Android进阶的一些知识总结&#xff0c;涉及到的知识点比较杂&#xff0c;不过都 是面试中几乎常问的知识点&…

海明距离

处理 非递减或者非递增 排列 的时候 &#xff0c;可以使用计数排序&#xff0c;将时间 复杂度变为 O&#xff08;N&#xff09;&#xff0c;空间复杂度变为O&#xff08;1&#xff09;。 1 int heightChecker(vector<int>& heights) {2 vector<int> res(10…

我们究竟还要学习哪些Android知识?满满干货指导

咸鱼翻身不断上演 2018年5月份&#xff0c;北京&#xff0c;在所谓的互联网寒冬里&#xff0c;一个普通二本的学生&#xff0c;在小公司工作一年后&#xff0c;跳槽拿到了百度的offer&#xff0c;月薪从9k变为17k&#xff0c;涨薪幅度接近100%。 2018年底&#xff0c;上海&am…

ElasticSearch6.3脚本更新

使用上篇文章创建的索引进行学习&#xff1a;https://www.cnblogs.com/wangymd/p/11200996.html 官方文档&#xff1a;https://www.elastic.co/guide/en/elasticsearch/painless/6.3/painless-examples.html 1、脚本更新指定字段 方式1&#xff1a; POST test_index/test_type…

我们究竟还要学习哪些Android知识?看这一篇就够了!

雪上加霜 本人一名Android程序员&#xff0c;今年29岁了。大厂小厂都呆过&#xff0c;现在在腾讯工作&#xff01;明明工作顺利&#xff0c;家庭和睦儿女成全&#xff0c;但是总是会感觉到&#xff0c;一股无形的压力&#xff0c;推着我走&#xff01;作为一名程序员我最怕的不…