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

來源:互聯網
上載者:User
文章目錄
  • BindingHelperExtensions.UpdateFrom改為UpdateModel
  •  

 

 

下載: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才可以訪問

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.