2014-11-01 5 views
1

lib.web.mvc를 사용할 때 ModelBinder를 만드는 이점은 무엇입니까? ?lib.web.mvc 용 ModelBinder의 이점

2011 년이 예제는 ModelBinder를

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.com/2011/03/jqgrid-and-aspnet-mvc-strongly-typed.html 그러나 최근의 예를 사용하지 않는 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 Form (이름/값 쌍을 사용하여)에서 반환 된 데이터로 새 인스턴스를 채 웁니다. 3) 제어기의 첫 x 째 행이 실행되기 전에 오브젝트 (필드)의 유효성 검증이 _ 생합니다. 4) MVC 모델 바인딩은 필드 데이터가 누락 된 경우 오류를 던지지 않습니다. 게시 된 양식 필드에 필요한 모든 것이 있어야합니다.

마지막으로 위에서 설명한 기능을 사용하면 사용자 정의 바인더를 작성하지 않고도 먼 길을 갈 수 있습니다. MVC는 전체보기를 지원하는 훌륭한 작업을 수행하기 때문에 강력하게 형식화 된 뷰와 뷰 모델을 사용합니다. 보기 모델에 바인딩.