我有一个窗体,张贴一堆隐藏的变量,一些具有相同的名称。这里的帖子正文: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