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:

 

代码

 

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:

 

代码

 

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

        }
            
复制代码

 

 

 

我的ASP.NET MVC实践系列

ASP.NET MVC实践系列1-UrlRouting

ASP.NET MVC实践系列2-简单应用

ASP.NET MVC实践系列3-服务器端数据验证

ASP.NET MVC实践系列4-Ajax应用

ASP.NET MVC实践系列5-结合jQuery

ASP.NET MVC实践系列6-Grid实现(上)

ASP.NET MVC实践系列7-Grid实现(下-利用Contrib实现)

ASP.NET MVC实践系列8-对查询后分页处理的解决方案

ASP.NET MVC实践系列9-filter原理与实践

ASP.NET MVC实践系列10-单元测试

ASP.NET MVC实践系列11-FCKEditor和CKEditor的使用

其他:

在ASP.NET MVC中对表进行通用的增删改

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值