C#ASP.NET Core ModelBinder不更新模型 [英] C# ASP.NET Core ModelBinder not Updating Model

查看:393
本文介绍了C#ASP.NET Core ModelBinder不更新模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个ModelBinder,它仅在对象分配了 [Decimal] 属性时才被触发,尽管出于某种原因,尽管它实际上对它所做的数据进行了消毒

I have created a ModelBinder, which only gets triggered if an object has a [Decimal] attribute assigned, yet for some reason, despite it actually sanitising the data it does not seem to update the posted model.

我想知道是否有人可以从下面的代码中看到我可能出问题的地方。

I wonder if someone could see from my code below, where I maybe going wrong.

Startup.cs

public void ConfigureServices(IServiceCollection serviceCollection)
{
    serviceCollection.AddMvc(config => config.ModelBinderProviders.Insert(0, new DecimalModelBinderProvider()));        
}

DecimalModelBinderProvider.cs

public class DecimalModelBinderProvider : IModelBinderProvider
{
    public IModelBinder GetBinder(ModelBinderProviderContext modelBinderProviderContext)
    {
        if (modelBinderProviderContext == null)
        {
            throw new ArgumentNullException(nameof(modelBinderProviderContext));
        }

        if (!modelBinderProviderContext.Metadata.IsComplexType)
        {
            try
            {
                var propertyName = modelBinderProviderContext.Metadata.PropertyName;

                var property = modelBinderProviderContext.Metadata.ContainerType.GetProperty(propertyName);

                if (property != null)
                {
                    var attribute = property.GetCustomAttributes(typeof(DecimalAttribute), false).FirstOrDefault();

                    if (attribute != null)
                    {
                        return new DecimalModelBinder(modelBinderProviderContext.Metadata.ModelType, attribute as IDecimalAttribute);
                    }
                }
            }
            catch (Exception exception)
            {
                var message = exception.Message;

                return null;
            }
        }

        return null;
    }
}

DecimalModelBinder.cs

public class DecimalModelBinder : IModelBinder
{
    private readonly IDecimalAttribute _decimalAttribute;

    private readonly SimpleTypeModelBinder _simpleTypeModelBinder;

    public DecimalModelBinder(Type type, IDecimalAttribute decimalAttribute)
    {
        if (type == null)
        {
            throw new ArgumentNullException(nameof(type));
        }

        _decimalAttribute = decimalAttribute;

        _simpleTypeModelBinder = new SimpleTypeModelBinder(type);
    }

    public Task BindModelAsync(ModelBindingContext modelBindingContext)
    {
        if (modelBindingContext == null)
        {
            throw new ArgumentNullException(nameof(modelBindingContext));
        }

        var valueProviderResult = modelBindingContext.ValueProvider.GetValue(modelBindingContext.ModelName);

        if (valueProviderResult != ValueProviderResult.None)
        {
            modelBindingContext.ModelState.SetModelValue(modelBindingContext.ModelName, valueProviderResult);

            var value = valueProviderResult.FirstValue;

            bool success;

            var result = _decimalAttribute.Decimal(value, out success);

            if (success)
            {
                modelBindingContext.Result = ModelBindingResult.Success(result);

                return Task.CompletedTask;
            }
        }

        return _simpleTypeModelBinder.BindModelAsync(modelBindingContext);
    }
}

IDecimalAttribute.cs

public interface IDecimalAttribute
{
    object Decimal(string value, out bool success);
}

DecimalAttribute.cs

[AttributeUsage(AttributeTargets.Property)]
public class DecimalAttribute : Attribute, IDecimalAttribute
{
    public object Decimal(string value, out bool success)
    {
        var tryModelValue = string.IsNullOrEmpty(value) ? "0.00" : value.Replace("£", "").Replace("%", "");

        decimal @decimal;

        success = decimal.TryParse(tryModelValue, out @decimal);

        return @decimal;
    }
}

Test.cs

public class Test
{
    [Display(Name = "Name", Description = "Name")]
    public string Name { get; set; }

    [Decimal]
    [Display(Name = "Amount", Description = "Amount")]
    public double Amount { get; set; }
}

HomeController

[ValidateAntiForgeryToken]
[HttpPost]
public IActionResult Index(Test test)
{
    if (ModelState.IsValid)
    {

    }

    return View(test);
}

为了进行测试,我将在金额字段中输入值252.83英镑

For the purpose of testing I will enter the value £252.83 into the Amount field and submit the form.

如果我然后将制动点放在 var value = valueProviderResult.FirstValue; 行上可以看到值是252.83英镑,如果我在 modelBindingContext.Result = ModelBindingResult.Success(result); 行上放置一个断点,我可以看到结果是 252.83M

If I then place a brakepoint on the line var value = valueProviderResult.FirstValue; I can see that value is £252.83 and if I place a breakpoint on the line modelBindingContext.Result = ModelBindingResult.Success(result); I can see that the result is 252.83M.

但是如果我进一步单步执行代码并将断点放在行上,如果(ModelState.IsValid)有效状态为false,如果我检查模型 test 对象 Amount 为0。

However if I step through the code further and place a breakpoint on the line if (ModelState.IsValid) the valid state is false and if I inspect the model test the object Amount is 0.

如果有人可以帮助,将不胜感激:-)

If anyone can help it would be much appreciated :-)

推荐答案

尝试进一步检查ModelState错误,Amount属性应该无效,并且必须存在异常。

Try inspect further on the ModelState Error, the Amount property should be invalid and there must be an exception.

我猜应该是InvalidCastException。我注意到当您在DecimalAttribute中生成Decimal时,Test类中的Amount属性为Double。

I am guessing it should be InvalidCastException. I notice that the Amount property in Test class is Double while you are producing Decimal in your DecimalAttribute.

因此,处理Test类的内置Model Binder(应为ComplexTypeModelBinder)无法设置Amount属性,因为它是另一种类型。

So the build-in Model Binder that processing the Test class (should be ComplexTypeModelBinder) unable to set the Amount property as it is different type.

这篇关于C#ASP.NET Core ModelBinder不更新模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆