不能添加新的自定义模型绑定?错误使用相同的键的项已添加 [英] cant add new custom Model Binder ? error An item with the same key has already been added

查看:151
本文介绍了不能添加新的自定义模型绑定?错误使用相同的键的项已添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我米尝试添加一个新的自定义模型绑定,但没有成功。下面是我的code和基础工作的长期(删除格式和转换2,345.34至234534),它是做精:

I m trying to add a new custom Model Binder but not successful. Below is my code and in term of basic working (removing formatting and converting 2,345.34 to 234534) it is doing fine:

 public class TransactionModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext,
            ModelBindingContext bindingContext)
        {
            ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue("Transaction.Price");
            ModelState modelState = new ModelState { Value = valueResult };
            object actualValue = null;
            object newValue = null;
            try
            {
                if (!string.IsNullOrEmpty(valueResult.AttemptedValue))
                {
                    newValue = valueResult.AttemptedValue.Replace(",", "");
                }
                actualValue = Convert.ToDecimal(newValue,
                    CultureInfo.CurrentCulture);
            }
            catch (FormatException e)
            {
                modelState.Errors.Add(e);
            }

            bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
            return actualValue;
        }
    }

我的Global.asax code为如下:

My Global.asax code is given below:

 protected void Application_Start()
    {
        ModelBinders.Binders.Add(typeof(decimal), new CurrencyModelBinder());
        ModelBinders.Binders.Add(typeof(decimal), new TxTransactionModelBinder());

        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
       // RouteTable.Routes.MapHubs();


    }

问题:
在运行code TransactionModelBinder做工精细,但在Global.asax中我得到了follwoing错误:

Issue: On running code TransactionModelBinder work fine but in global.asax I got the follwoing error:

具有相同键的项已被添加。

我能理解 typeof运算(十进制)在两个定制粘合剂是造成这个问题。

I can understand typeof(decimal) in two custom binders is causing this issue.

您可以请引导和帮助我如何解决这个问题。

Can you please guide and help me on how to fix this.

推荐答案

您只能有每个数据类型的单一模式粘合剂。我不能完全肯定的区别是两个粘合剂之间什么,但也许你可以他们的逻辑组合成一个单一的模型粘合剂。如果你确实需要两个不同的模型粘合剂,那么也许你需要创建具体到每个模型绑定两个不同的数据类型。

You can only have a single model binder per datatype. I'm not entirely sure what the difference is between the two binders, but perhaps you could combine their logic into a single model binder. If you absolutely need two different model binders, then perhaps you need to create two different datatypes specific to each model binder.

这篇关于不能添加新的自定义模型绑定?错误使用相同的键的项已添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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