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" />
<input type="submit" value="提交" />
</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

复制代码

代码

<%using (Html.BeginForm("HtmlCheckBox", "Home"))
      {%>
<%=Html.CheckBox("lfm1") %>
<%=Html.CheckBox("lfm2") %>
<%=Html.CheckBox("lfm3") %>
<input type="submit" value="提交" />
<%} %>

复制代码

Controller:

代码

1 public ActionResult HtmlCheckBox(FormCollection formCollection)
2         {
3 string result = "选中的为:";
4             var select = from x in formCollection.AllKeys
5 where formCollection[x].Contains("true")
6                           select x;
7 foreach (var item in select)
8             {
9                 result = result + item;
10             }
11             var unSelect = from x in formCollection.AllKeys
12 where formCollection[x]=="false"
13                            select x;
14             result = result + ",未选中的为:";
15 foreach (var item in unSelect)
16             {
17                 result = result + item;
18             }
19 return Content(result);
20         }

2、<input type="checkbox">

我们先添加一个View:

复制代码

视图

<%using (Html.BeginForm("TestCheckBox", "Home"))
  {%>
<input type="checkbox" name="lfm" value="1" />
<input type="checkbox" name="lfm" value="2" />
<input type="checkbox" name="lfm" value="3" />
<input type="checkbox" name="lfm" value="4" />
<input type="checkbox" name="lfm" value="5" />
<input type="submit" value="提交" />
<%} %>

复制代码

这里要注意所有的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:

复制代码

View

<%using (Html.BeginForm("RadioResult","Home"))
  { %>
<%=Html.RadioButton("lfm", "1")%>
<%=Html.RadioButton("lfm", "2")%>
<input type="submit" value="提交" />
<%} %>

复制代码

Controller:

复制代码

代码

public ActionResult HtmlRadioButtonTest()
        {
return View();
        }
public ActionResult RadioResult()
        {
return Content(Request["lfm"]);
        }

复制代码

得到的结果就是你选中RadioButton的值。这个helper和 <input type="radio" value=1 />版本用法是基本一样的。
四、应用

View:

代码

<h2>NewsList</h2>
<table>
<tr>
<th>
                选择
</th>
<th>
                Author
</th>
<th>
                Title
</th>
<th>
                CreateTime
</th>
</tr>
<%using (Html.BeginForm("NewsListResult", "Home"))
  {%>
<% foreach (var item in Model)
       { %>
<tr>
<td>
<input type="checkbox" name="ID" value="<%=item.ID%>" />
</td>
<td>
<%= Html.Encode(item.Author)%>
</td>
<td>
<%= Html.Encode(item.Title)%>
</td>
<td>
<%= Html.Encode(String.Format("{0:g}", item.CreateTime))%>
</td>
</tr>
<% } %>
<input type="submit" value="提交" />
<%} %>
</table>

Controller:

复制代码

代码

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);
        }

转自:http://www.cnblogs.com/nuaalfm/archive/2009/11/30/1613712.html

原文地址:https://www.cnblogs.com/luckjason/p/2526156.html