2014-11-01 41 views
1

使用lib.web.mvc時創建ModelBinder有什麼好處。 ?ModelBinder for lib.web.mvc的好處

從2011年這個例子不使用ModelBinder的

http://tpeczek.com/2011/03/jqgrid-and-aspnet-mvc-strongly-typed.html

public class ProductViewModel 
{ 
    #region Properties 
    public int Id { get; set; } 

    public string Name { get; set; } 

    [JqGridColumnSortingName("SupplierId")] 
    public string Supplier { get; set; } 

    [JqGridColumnSortingName("CategoryId")] 
    public string Category { get; set; } 

    [DisplayName("Quantity Per Unit")] 
    [JqGridColumnAlign(JqGridColumnAligns.Center)] 
    public string QuantityPerUnit { get; set; } 

    [DisplayName("Unit Price")] 
    [JqGridColumnAlign(JqGridColumnAligns.Center)] 
    public decimal? UnitPrice { get; set; } 

    [DisplayName("Units In Stock")] 
    [JqGridColumnAlign(JqGridColumnAligns.Center)] 
    public short? UnitsInStock { get; set; } 
    #endregion 

    #region Constructor 
    public ProductViewModel() 
    { } 

    public ProductViewModel(Product product) 
    { 
    this.Id = product.Id; 
    this.Name = product.Name; 
    this.Supplier = product.Supplier.Name; 
    this.Category = product.Category.Name; 
    this.QuantityPerUnit = product.QuantityPerUnit; 
    this.UnitPrice = product.UnitPrice; 
    this.UnitsInStock = product.UnitsInStock; 
    } 
    #endregion 
} 

但最新的例子是使用它們

http://tpeczek.codeplex.com/SourceControl/latest#trunk/ASP.NET%20MVC%20Examples/jqGrid%20Examples/jqGrid/Models/ProductViewModel.cs

namespace jqGrid.Models 
{ 
    [ModelBinder(typeof(ProductViewModelBinder))] 
    public class ProductViewModel 
    { 
     #region Properties 
     public int? ProductID { get; set; } 

     public string ProductName { get; set; } 

     public int SupplierID { get; set; } 

     public int CategoryID { get; set; } 

     public string QuantityPerUnit { get; set; } 

     public decimal UnitPrice { get; set; } 

     public short UnitsInStock { get; set; } 
     #endregion 
    } 

public class ProductViewModelBinder : DefaultModelBinder 
    { 
     public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
     { 
      ProductViewModel model = (ProductViewModel)base.BindModel(controllerContext, bindingContext); 

      if (controllerContext.HttpContext.Request.Params["id"] != "_empty") 
       model.ProductID = Convert.ToInt32(controllerContext.HttpContext.Request.Params["id"]); 
      model.SupplierID = Convert.ToInt32(controllerContext.HttpContext.Request.Params["Supplier"]); 
      model.CategoryID = Convert.ToInt32(controllerContext.HttpContext.Request.Params["Category"]); 
      model.UnitPrice = Convert.ToDecimal(controllerContext.HttpContext.Request.Params["UnitPrice"].Replace(".", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator)); 

      return model; 
     } 
    } 

回答

0

模型綁定提供的一個MV最便捷的功能C必須提供。模型綁定器的主要工作是將HTML查詢字符串轉換爲強類型。開箱即用,MVC模型活頁夾做得非常出色,但是您可能擁有一個強大的類型,它不適用於默認的活頁夾。在這種情況下,您可以創建自己的。或者,您可以自定義它們,例如,回傳可能只包含一個字符串值,您希望返回整個類,甚至包含視圖模型。

要記住默認行爲的幾件事情是:1)MVC新聞上的實例是它自己的操作方法,它採用模型或視圖模型類的第一個參數! 2)然後它將嘗試使用查詢字符串的Web窗體(使用名稱/值對)返回的數據填充新實例。 3)驗證對象(字段)發生在控制器的第一行之前。 4)如果缺少字段數據,MVC模型綁定不會引發錯誤(請確保您發佈的表單字段包含您需要的所有內容。)

最後,使用上述功能,您可以在不編寫自定義綁定器的情況下走很長的路。他們在那裏可以處理邊緣案例或者想要實現的任何「技巧」,以使您的應用程序精益求精。對我而言,我幾乎總是使用強類型視圖和視圖模型,因爲MVC在支持完整視圖方面做得非常出色綁定查看模型。