我有一個窗體,張貼一堆隱藏的變量,一些具有相同的名稱。這裏的帖子正文:MVC 3自定義ModelBinder不被使用
OfferId=3802&DeliveryDate=11%2F02%2F2011&DeliveryTime=12%3A00&location=1&location=698
它,它發佈到一個MVC動作:
[HttpPost]
public ActionResult DeliveryOptions(DeliveryOptions model)
{
...
}
的DeliveryOptions模型是這樣的:
public class DeliveryOptions
{
public long? OfferId { get; set; }
[CustomValidation(typeof(CustomValidator), "IsDeliveryDateValid")]
public DateTime? DeliveryDate { get; set; }
public DateTime? DeliveryTime { get; set; }
[CustomValidation(typeof(CustomValidator), "IsLocationsValid")]
public OfferLocations Locations { get; set; }
}
現在,我想解析貼location
變量轉換爲OfferLocations對象,如下所示:
[ModelBinder(typeof(OfferLocationsModelBinder))]
public class OfferLocations
{
[Required]
public int[] LocationIds { get; set; }
}
模型綁定目前看起來是這樣的:
public class OfferLocationsModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
throw new NotImplementedException();
}
}
的問題是,我不能在NotImplementedException突破。模型聯編程序不執行。我可能錯過了一些明顯的東西;有任何想法嗎?
我試圖在全球註冊模型綁定.asax Application_Start,但沒有幫助。另外,項目中還有其他幾個自定義模型粘合劑,它們看起來很像我的,它們也可以工作。我只是不明白爲什麼我的沒有。我是否需要將'location'後綴變量重命名爲其他內容? – Edgar