Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未使用MVC 3自定义ModelBinder_C#_Asp.net Mvc 3_Modelbinders - Fatal编程技术网

C# 未使用MVC 3自定义ModelBinder

C# 未使用MVC 3自定义ModelBinder,c#,asp.net-mvc-3,modelbinders,C#,Asp.net Mvc 3,Modelbinders,我有一个表单,它发布了一堆隐藏变量,其中一些具有相同的名称。这是帖子的主体: OfferId=3802&DeliveryDate=11%2F02%2F2011&DeliveryTime=12%3A00&location=1&location=698 它将其发布到MVC操作: [HttpPost] public ActionResult DeliveryOptions(DeliveryOptions model) { ... } DeliveryOpti

我有一个表单,它发布了一堆隐藏变量,其中一些具有相同的名称。这是帖子的主体:

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; }
}
[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();
    }
}
现在,我想将posted
location
变量解析为OfferLocations对象,如下所示:

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; }
}
[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();
    }
}
模型活页夹当前看起来如下所示:

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; }
}
[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。模型绑定器不执行。我可能错过了一些明显的东西;有什么想法吗?

ModelBinder是否已向DependencyResolver注册?我想知道MVC是否在查找ModelBinder时遇到了问题,并且正在使用默认的ModelBinder。

无法实现此功能。必须实施一个变通办法


我现在将位置ID作为CSV字符串发布,它们由自定义模型绑定器拾取,因此可以解析到数组中。遗憾的是,我不能用另一种方法来做。

尝试从
System.Web.Mvc.DefaultModelBinder
继承模型绑定器,并重写
对象绑定模型(ControllerContext,ModelBindingContext)
方法。看看这是否有效。如果有,您可以从那里开始工作:)

这就是我现在正在做的。基本上,我必须确保如果请求中出现了特定类型,则会针对该值执行一个方法。模型绑定器注册代码完全相同-一个应用于属性类的属性

模型活页夹如下所示:

public class TableModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var result = base.BindModel(controllerContext, bindingContext) as ITableModel;
        if (result != null)
            result.UpdateSorter();
        return result;
    }
}
从基本模型绑定器派生的p.S.为我提供了使用标准Mvc代码绑定所有属性的额外好处,然后我可以扩展反序列化对象:)


希望这有帮助

我尝试在Global.asax应用程序\u Start中注册模型绑定器,但没有帮助。此外,项目中还有其他几个自定义模型绑定器,它们看起来与我的非常相似,并且可以正常工作。我只是不明白为什么我的没有。是否需要将
位置
post变量重命名为其他变量?在我的解决方法中使用了此方法。因为我将发布的变量更改为CSV字符串,所以工作得非常好。