Asp.net MVC2中你必須知道的擴充點(二):Model Binder

來源:互聯網
上載者:User

    Model Binder在Asp.net MVC中非常簡單。簡單的說就是你控制器中的Action方法需要參數資料;而這些參數資料包含在HTTP請求中,

包括表單上的Value和URL中的參數等。而ModelBinder的功能就是將這些個表單上的Value和URL中的參數換成對象,然後將這些對象綁定

到Action的參數上面。我簡單的畫了一個圖,看起來會更加直觀。

 

在asp.net mvc中你可以寫類似下面這樣的代碼:

[HttpPost]public ActionResult Create(){    Book book = new Book();    book.Title = Request.Form["Title"];    // ...    return View();}

但是這樣的寫法是非常不可取的,因為代碼不容易閱讀,也不易測試。再看下面的寫法:

[HttpPost]public ActionResult Create(FormCollection values){    Book book = new Book();    book.Title = values["Sex"];    // ...    return View();}

    這樣的寫法就可以不用從Request中擷取資料了,這樣能滿足一些情況,比直接從Request中擷取資料要直觀。但是如果在Action需要的

資料既要來自表單上的值,又要來自URL的query string。這種情況單單FormCollection是不行的。看下面代碼:

[HttpPost]public ActionResult Create(Book book){    // ...    return View();}    上面的代碼就非常的直觀了,這需要我們的model binder建立一個book對象,然後直接從這個對象的屬性中取值。這個book對象的數
據自然也是來自Form和URL。有時候,我們的DefaultModelBinder轉換的能力必經有限,也不夠透明化,一些特殊和複雜的情況就需要我
們自訂Model Binder。下面我講講如何去自訂Model Binder。

1、首先我們定義一個Book的實體類:

public class Book{    public string Title { get; set; }    public string Author { get; set; }    public DateTime DatePublished { get; set; }}

2、自訂的model binder需要繼承IModelBinder或者它的子類。資料可以從bindingContext擷取。

public class BookModelBinder : IModelBinder{    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)    {        var book = (Book)(bindingContext.Model ?? new Book());        book.Title = GetValue<string>(bindingContext, "Title");        book.Author = GetValue<string>(bindingContext, "Author");        book.DatePublished = GetValue<DateTime>(bindingContext, "DatePublished");        if (String.IsNullOrEmpty(book.Title))        {            bindingContext.ModelState.AddModelError("Title", "書名不可為空?");        }        return book;    }    private T GetValue<T>(ModelBindingContext bindingContext, string key)    {        ValueProviderResult valueResult= bindingContext.ValueProvider.GetValue(key);        bindingContext.ModelState.SetModelValue(key, valueResult);        return (T)valueResult.ConvertTo(typeof(T));    }}

從上面代碼可以看出,自訂的ModelBinde非常的自由,可以自由的將Form上的一個key對應實體的一個屬性,
也可以加入一些驗證的邏輯。當然還可以加入一些其他的自訂邏輯。
3、寫好BookModelBinder之後,我們只需要簡單的註冊一下就行了,在Global.asax添加下面代碼:
ModelBinders.Binders.Add(typeof(Book), new BookModelBinder());

總結:本文簡單介紹了一下Asp.net MVC的Model Binder機制。如果敘述有問題,歡迎指正。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.