1

使用 lib.web.mvc 时创建 ModelBinder 有什么好处。?

这个 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;
        }
    }
4

1 回答 1

0

Model Binders provide one of the most convenient functions that MVC has to offer. Model Binders' main job is to convert HTML Query Strings to strong-types. Out of the box, MVC model binders do an excellent job, but you may have a strong-type that doesn't work with the default binders. In that case you can create your own. Or you can customize them for example, maybe the postback only contains a single string value that you want to return an entire class or even a viewmodel packed with stuff.

Couple of things to keep in mind with the default behavior are: 1) MVC news up instances on it's own of Action Methods that take a first parameter of a model or view model class! 2) It will then attempt to populate that new instance with data returned from the Web Form (using Name/Value pairs) of the Query string. 3) Validation of the object (fields) happens before the first line in the controller is executed. 4) MVC model binding will NOT throw an error if there is missing field data (make sure your posted form fields have everything you need.

Finally with the functionality described above you can go a long way without writing custom binders. However they are there to handle the edge cases or any "tricks" you want to implement to make your application lean and mean. For me, I almost always use strongly-typed views and view-models as MVC does a great job of supporting full view binding to view models.

于 2014-11-01T16:15:52.590 回答