天天看点

ASP.NET MVC实践系列12-表单处理

其实这应该算是一个很简单的话题,但是由于webform为我们封装的太多,很多人对这部分的原理并不是特别清楚,搞得这个表单提交在ASP.NET MVC中好像很神秘似得,下面我就来帮大家揭揭秘,当然高手就别看了会浪费你的时间的。

一、基础知识

HTTP请求有两种方式GET与POST,理论上说,GET是从服务器上请求数据,POST是发送数据到服务器。事实上,GET方法是把数据参数队列(query string)加到一个URL上,名字和值是一一对应的。比如说,name=lfm。在队列里,各组数据用一个&符号分开,空格用+号替换,特殊的符号转换成十六进制的代码。因为这一队列在URL里边,这样队列的参数就能看得到,可以被记录下来,或更改。通常GET方法还限制字符的大小(我测试了一下大概4083,大于4083直接被截断,不报错 )。事实上POST方法可以没有限制的传递数据到服务器,用户在浏览器端是看不到这一过程的。接下来要讲的这个表单提交使用的就是post方式。

二、CheckBox

1、Html.CheckBox:这是ASP.NET MVC中提供的一个Helper,它会生成一个input-checkbox和一个同名的input-hidden,比方说我们的view为:

代码

<%using (Html.BeginForm("HtmlCheckBox", "Home"))

      {%>

    <%=Html.CheckBox("lfm1") %>

    <input type="submit" value="提交" />

    <%} %>

则会生成:

  <form action="/Home/HtmlCheckBox" method="post">

<input id="lfm1" name="lfm1" type="checkbox" value="true" />

<input name="lfm1" type="hidden" value="false" />

    </form>

为什么要有这么一个input-hidden呢,我们都知道checkbox在提交时如果不选中则将不被提交到服务器端,所以有时我们可能希望如

果不选中也提交到服务器端,这个input-hidden就是干这个的,不过这里实现的有点蹩脚,当选中时会有两个同名的表单项,一个值

为TRUE,一个值为FALSE。

当controller为如下时:

 public ActionResult HtmlCheckBox(FormCollection formCollection)

        {

            return Content(Request["lfm1"]);

        }

如果选中checkbox,则得到“true,false”,如果不选中checkbox则得到“false”。如果你需要用到不选也要提交的情况可以使用

这个helper,但一般还是推荐大家直接使用<input type="checkbox">。

 对于这个helper的使用大家可以参考如下代码:

View

<a></a>

    &lt;%using (Html.BeginForm("HtmlCheckBox", "Home"))

    &lt;%=Html.CheckBox("lfm2") %&gt;

     &lt;%=Html.CheckBox("lfm3") %&gt;

Controller:

2、&lt;input type="checkbox"&gt;

我们先添加一个View:

视图

&lt;%using (Html.BeginForm("TestCheckBox", "Home"))

  {%&gt;

   &lt;input type="checkbox" name="lfm" value="1" /&gt;

    &lt;input type="checkbox" name="lfm" value="2" /&gt;

     &lt;input type="checkbox" name="lfm" value="3" /&gt;

      &lt;input type="checkbox" name="lfm" value="4" /&gt;

       &lt;input type="checkbox" name="lfm" value="5" /&gt;

       &lt;input type="submit" value="提交" /&gt;

&lt;%} %&gt;

这里要注意所有的Input的name都相同,Html.BeginForm("TestCheckBox", "Home")的意思是当此表单提交的时候会提交到

HomeController的TestCheckBox这个Action方法中。我们添加这个方法:

Action

 public ActionResult TestCheckBox(int[] lfm)

         {

             string t="";

             foreach (var item in lfm)

             {

                 t = t + item.ToString();

             }

             return Content(t);

         }

为了简单,TestCheckBox方法的参数名需要与input的名字相同,于是mvc自动帮我们将你选择的checkbox的value值匹配到参数数组

中,当然你可以通过Request获得,不过这样的话需要你自己进行处理。

三、RadioButton

RadioButton的用法就比较简单了,我们举个简单的例子:

View:

&lt;%using (Html.BeginForm("RadioResult","Home"))

  { %&gt;

    &lt;%=Html.RadioButton("lfm", "1")%&gt;

 &lt;%=Html.RadioButton("lfm", "2")%&gt;

 &lt;input type="submit" value="提交" /&gt;

 &lt;%} %&gt;

Controller:

 public ActionResult HtmlRadioButtonTest()

        {

            return View();

        }

        public ActionResult RadioResult()

            return Content(Request["lfm"]);

得到的结果就是你选中RadioButton的值。这个helper和 &lt;input type="radio" value=1 /&gt;版本用法是基本一样的。

四、应用

public ActionResult NewsList()

            return View(ListNews.GetList());

        public ActionResult NewsListResult(int[] ID)

            string result = "选中的ID";

            foreach (var item in ID)

            {

                result = result + "," + item;

            }

            return Content(result);

我的ASP.NET MVC实践系列

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/10/26/1589802.html" target="_blank">ASP.NET MVC实践系列1-UrlRouting</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/10/26/1590064.html" target="_blank">ASP.NET MVC实践系列2-简单应用</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/10/27/1590532.html" target="_blank">ASP.NET MVC实践系列3-服务器端数据验证</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/10/28/1591410.html" target="_blank">ASP.NET MVC实践系列4-Ajax应用</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/11/04/1595411.html" target="_blank">ASP.NET MVC实践系列5-结合jQuery</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/11/10/1596864.html" target="_blank">ASP.NET MVC实践系列6-Grid实现(上)</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/11/12/1601959.html" target="_blank">ASP.NET MVC实践系列7-Grid实现(下-利用Contrib实现)</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/11/18/1605364.html" target="_blank">ASP.NET MVC实践系列8-对查询后分页处理的解决方案</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/11/24/1609752.html" target="_blank">ASP.NET MVC实践系列9-filter原理与实践</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/11/26/1611420.html" target="_blank">ASP.NET MVC实践系列10-单元测试</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/11/27/1612027.html" target="_blank">ASP.NET MVC实践系列11-FCKEditor和CKEditor的使用</a>

其他:

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/11/11/1600811.html" target="_blank">在ASP.NET MVC中对表进行通用的增删改</a>

本文转自 你听海是不是在笑 博客园博客,原文链接:http://www.cnblogs.com/nuaalfm/archive/2009/11/30/1613712.html  ,如需转载请自行联系原作者

继续阅读