Asp.net Mvc Codeplex Preview 5 第二篇 Controller&Filter的新特性

 

下载:http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=16775

上篇: Asp.net Mvc Codeplex Preview 5 第一篇 Helper的新特性

 

 

BindingHelperExtensions.UpdateFrom改为UpdateModel

使用方法(缺点:如果表单里存在MyModel里不存在的字段就会报错)
var x 
=   new  MyModel();
this .TryUpdateModel(x, Request.Form.AllKeys);

或(缺点:一个一个写太麻烦)
var x 
=   new  MyModel();
this .TryUpdateModel(x,  new [] { " IDX " " Name " });

优点:如果你有定义HtmlHelper.ValidationMessage那么它将会自动为你判空,详见上篇文章

 当然,你也可以通过this.TryUpdateModel(x, new[] {"IDX""Name"},"Account");这种方式来获取不同的Model.

上述 TryUpdateModel返回是否绑定成功的bool值,如果确定无误,可使用 UpdateModel

Action提供了数组参数

View:

   <% using (Html.Form( " home " " save " , FormMethod.Post)) { %>
     
<% = Html.CheckBox( " like " %>
     
<% = Html.CheckBox( " like " %>
     
<% = Html.CheckBox( " like " %>
     
<% = Html.CheckBox( " like " %>
     
<% = Html.SubmitButton()  %>
     
<% } %>

Action:

      public  ActionResult Save( string [] like)
         {
              ViewData[
" Message " =   string .Join( "" , like);
              
return  View( " Index " );
         }


这样是可以取到like的值的

 

AcceptVerbs Filter

这个Filter可以让我们定义Action的访问方式


[AcceptVerbs( " Post " )]
public  ActionResult Save()//只有Post才能访问

[AcceptVerbs(
" Post " , " GET " )]//Post或Get都能访问,但其它方式不能访问
public  ActionResult Save()

 

 

ActionName Filter

为一个方法定义它的ActionName

如HomeController中定义


         [ActionName( " MyAction " )]
         
public  ActionResult Save()
         {
              
return  View( " Index " );
         }

 

则/home/save不能访问

一定要/home/myaction才可以访问

你可能感兴趣的:(Asp.net Mvc Codeplex Preview 5 第二篇 Controller&Filter的新特性)