2014-02-12 105 views
0

餘米試圖創建一個自定義的模型綁定與下面的代碼:如何在自定義模型綁定中獲取模型?

public class TransactionModelBinder : IModelBinder 
{ 
    public object BindModel(ControllerContext controllerContext, 
     ModelBindingContext bindingContext) 
    { 
     Object Model = bindingContext.Model; 

     //Do custom logic 

     return Model; 
    } 
} 

在Global.asax中,我米添加:

ModelBinders.Binders.Add(typeof(TransViewModel), new TransactionModelBinder()); 

問題: 我不知道如何讓模型。我試過bindingContext.Model,但它是空的。 請同時指導我的Global.asax代碼行是否正常。

回答

1

好吧,如果你打算從頭開始編寫整個聯編程序,你將不會有模型。相反,你實際上是創建模型的人(因爲這是活頁夾的用途),從表單數據。如:

return new SomeModel 
{ 
    OneProp = request.Form["OneProp"], 
    AnotherProp = request.Form["AnotherProp"] 
} 

或者你可以從DefaultModelBinder而不是IModelBinder,你可以用它來擴展只有某些行爲,而不是實際處理模型的構建繼承。

編輯:

從您的意見,我的理解是,你只是想處理幾個的ViewModels一個屬性,你可能有(可能是多個的ViewModels有來自鑑於不同的格式小數什麼MVC期望小數。

在這種情況下,我實際上使用一種不同的方法。而不是在global.asax中註冊ModelBinder,我將它從那裏刪除,並在需要的實際屬性聲明性地做特殊格式。

如:

[PropertyBinder(typeof(MyDecimalBinder))] 
public decimal SomePropInAViewModel {get; set;} 

這是基於創建PropertyBindingAttribute的共同做法: http://www.prideparrot.com/blog/archive/2012/6/customizing_property_binding_through_attributeshttps://stackoverflow.com/a//1373170

並配有ModelBinder的與此類似:

public class MyDecimalBinder : DefaultModelBinder { 
    protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) { 

     // use the propertyDescriptor to make your modifications, by calling SetProperty() 
     ... 

     base.BindProperty(controllerContext, bindingContext, propertyDescriptor); 
    } 

} 

現在,如果這是你想要應用於所有小數點的東西,你可能也想看看菲爾哈克完全實現小數使用自定義綁定處理:

http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx/

+0

同意。如果你願意,可以多指點一點,非常感謝。你說的是從模型,我應該修改一個屬性和返回屬性,而不是完整的模型? 之前我之前嘗試過。只需修改一個小數屬性。 問題: 不幸的是,在Global.asax中,我已經有一個帶有十進制的客戶綁定。我試過: ModelBinders.Binders.Add(typeof(decimal),new CurrencyModelBinder()); ModelBinders.Binders.Add(typeof(decimal),new TxTransactionModelBinder()); 但錯誤「已添加具有相同密鑰的項目。「 – Toubi

+0

詳情請參閱 http://stackoverflow.com/questions/21717757/cant-add-new-custom-model-binder-error-an-item-with-the-same-key-has-already -b?noredirect = 1#comment32841219_21717757 – Toubi

+0

是否意味着在global.asax中,我可以註冊2個相同類型的自定義模型綁定器(在這種情況下,typeof(decimal))?我很欣賞你的方法,但只是想問一下,是不可能的?如果你必須從global.asax執行,你會怎麼做? – Toubi

2

看到這個article

public class HomeCustomDataBinder : DefaultModelBinder 
{ 

    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     if (bindingContext.ModelType == typeof(HomePageModels)) 
     { 
      HttpRequestBase request = controllerContext.HttpContext.Request; 

      string title = request.Form.Get("Title"); 
      string day = request.Form.Get("Day"); 
      string month = request.Form.Get("Month"); 
      string year = request.Form.Get("Year"); 

      return new HomePageModels 
      { 
       Title = title, 
       Date = day + "/" + month + "/" + year 
      }; 

      //// call the default model binder this new binding context 
      //return base.BindModel(controllerContext, newBindingContext); 
     } 
     else 
     { 
      return base.BindModel(controllerContext, bindingContext); 
     } 
    } 

} 
+0

謝謝。你是一個好幫手。 – Toubi