有没有办法让 DefaultModelBinder 在绑定到 List时忽略空项?

发布于 2024-08-22 19:47:08 字数 542 浏览 5 评论 0原文

我有一个场景,我想更改 DefaultModelBinder 绑定到枚举列表的方式的行为。

我有一个枚举...

public enum MyEnum { FirstVal, SecondVal, ThirdVal }

和一个模型类...

public class MyModel
{
    public List<MyEnum> MyEnums { get; set; }
}

并且 POST 正文是...

MyEnums=&MyEnums=ThirdVal

目前,在模型绑定之后,MyEnums 属性将包含...

[0] = FirstVal
[1] = ThirdVal

是否有一种方法可以告诉模型绑定程序忽略发布的数据中的空值,以便 MyEnums 属性看起来像下面这样?

[0] = ThirdVal

I have a scenario where I'd like to change the behavior of the DefaultModelBinder in how it binds to a List of enums.

I have an enum...

public enum MyEnum { FirstVal, SecondVal, ThirdVal }

and a class for a model...

public class MyModel
{
    public List<MyEnum> MyEnums { get; set; }
}

and the POST body is...

MyEnums=&MyEnums=ThirdVal

Currently, after model binding, the MyEnums property will contain...

[0] = FirstVal
[1] = ThirdVal

Is there was a way to tell the model binder to ignore the empty value in the posted data so that MyEnums property could look like the following?

[0] = ThirdVal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的快感 2024-08-29 19:47:08

您可以为 MyModel 编写一个自定义模型绑定器:

public class MyModelModelBinder : DefaultModelBinder
{
    protected override void SetProperty(
        ControllerContext controllerContext, 
        ModelBindingContext bindingContext, 
        PropertyDescriptor propertyDescriptor, 
        object value)
    {
        if (value is ICollection<MyEnum>)
        {
            var myEnums = controllerContext.HttpContext.Request[propertyDescriptor.Name];
            if (!string.IsNullOrEmpty(myEnums))
            {
                var tokens = myEnums.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                value = tokens.Select(x => (MyEnum)Enum.Parse(typeof(MyEnum), x)).ToList();
            }
        }
        base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
    }
}

它在 Application_Start 中注册:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
    ModelBinders.Binders.Add(typeof(MyModel), new MyModelModelBinder());
}

更新:

根据评论部分的要求,以下是如何使以前的绑定器更通用:

protected override void SetProperty(
    ControllerContext controllerContext, 
    ModelBindingContext bindingContext, 
    PropertyDescriptor propertyDescriptor, 
    object value)
{
    var collection = value as IList;
    if (collection != null && collection.GetType().IsGenericType)
    {
        var genericArgument = collection
            .GetType()
            .GetGenericArguments()
            .Where(t => t.IsEnum)
            .FirstOrDefault();

        if (genericArgument != null)
        {
            collection.Clear();
            var enumValues = controllerContext.HttpContext
                .Request[propertyDescriptor.Name];
            var tokens = enumValues.Split(
                new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var token in tokens)
            {
                collection.Add(Enum.Parse(genericArgument, token));
            }
        }
    }
    base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
}

You could write a custom model binder for MyModel:

public class MyModelModelBinder : DefaultModelBinder
{
    protected override void SetProperty(
        ControllerContext controllerContext, 
        ModelBindingContext bindingContext, 
        PropertyDescriptor propertyDescriptor, 
        object value)
    {
        if (value is ICollection<MyEnum>)
        {
            var myEnums = controllerContext.HttpContext.Request[propertyDescriptor.Name];
            if (!string.IsNullOrEmpty(myEnums))
            {
                var tokens = myEnums.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                value = tokens.Select(x => (MyEnum)Enum.Parse(typeof(MyEnum), x)).ToList();
            }
        }
        base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
    }
}

which is registered in Application_Start:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
    ModelBinders.Binders.Add(typeof(MyModel), new MyModelModelBinder());
}

UPDATE:

As requested in the comments section here's how to make the previous binder more generic:

protected override void SetProperty(
    ControllerContext controllerContext, 
    ModelBindingContext bindingContext, 
    PropertyDescriptor propertyDescriptor, 
    object value)
{
    var collection = value as IList;
    if (collection != null && collection.GetType().IsGenericType)
    {
        var genericArgument = collection
            .GetType()
            .GetGenericArguments()
            .Where(t => t.IsEnum)
            .FirstOrDefault();

        if (genericArgument != null)
        {
            collection.Clear();
            var enumValues = controllerContext.HttpContext
                .Request[propertyDescriptor.Name];
            var tokens = enumValues.Split(
                new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var token in tokens)
            {
                collection.Add(Enum.Parse(genericArgument, token));
            }
        }
    }
    base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文