MVC3 模型验证不适用于双精度

发布于 2024-11-02 17:37:31 字数 1560 浏览 2 评论 0原文

我在 MVC 中验证时遇到问题,我的模型有双重属性,当我提交 10.30 或任何带有“.”的内容时它里面告诉我“值‘10.30’对于价格无效”。 我做了一些研究,他们说模型验证应该是文化不变的,我认为这可能是问题,因为我的浏览器和服务器是法语,但它不应该。

这是我的代码:

[HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "Admin")]
        [ValidateInput(false)]
        public virtual ActionResult Edit(AuctionModel model)
        {
            if (ModelState.IsValid)
            {
                //do the work
            }
            return View(model);
        }

public class AuctionModel
    {
        public string Id { get; set; }
        [Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "FieldMandatory")]
        [LocalizedDisplayName("Title")]
        public string Title { get; set; }
        [Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "FieldMandatory")]
        [LocalizedDisplayName("Description")]
        public string Description { get; set; }
        [Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "FieldMandatory")]
        [LocalizedDisplayName("Photo")]
        public string Photo { get; set; }
        [Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "FieldMandatory")]
        [LocalizedDisplayName("StartDate")]
        public DateTime StartDate { get; set; }
        [Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "FieldMandatory")]
        [LocalizedDisplayName("Price")]
        public double Price { get; set; }
}

感谢您的帮助!

I have a problem with yhe validation in MVC, my model has a double property and when I submit 10.30 or anything with "." inside it tells me that "The value '10.30' is not valid for Price".
I did some research and they say that model validation should be Culture invariant, I was thinking that it could be the problem since my browser and server is in french but it should'nt.

Here's my code :

[HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "Admin")]
        [ValidateInput(false)]
        public virtual ActionResult Edit(AuctionModel model)
        {
            if (ModelState.IsValid)
            {
                //do the work
            }
            return View(model);
        }

public class AuctionModel
    {
        public string Id { get; set; }
        [Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "FieldMandatory")]
        [LocalizedDisplayName("Title")]
        public string Title { get; set; }
        [Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "FieldMandatory")]
        [LocalizedDisplayName("Description")]
        public string Description { get; set; }
        [Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "FieldMandatory")]
        [LocalizedDisplayName("Photo")]
        public string Photo { get; set; }
        [Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "FieldMandatory")]
        [LocalizedDisplayName("StartDate")]
        public DateTime StartDate { get; set; }
        [Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "FieldMandatory")]
        [LocalizedDisplayName("Price")]
        public double Price { get; set; }
}

Thanks for the help!

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

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

发布评论

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

评论(2

风吹短裙飘 2024-11-09 17:37:31

最后,我关注了 Haacked 的这篇文章:

http://haacked .com/archive/2011/03/19/fixing-binding-to-decimals.aspx

它工作得很好。

这是代码:

public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        var modelState = new ModelState { Value = valueResult };
        object actualValue = null;
        try
        {
            actualValue = Convert.ToDecimal(valueResult.AttemptedValue, CultureInfo.InvariantCulture);
        }
        catch (FormatException e)
        {
            modelState.Errors.Add(e);
        }

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

在 global.ascx 中:

ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());

Finaly I follow this post from Haacked :

http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx

And it works like fine.

Here's the code :

public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        var modelState = new ModelState { Value = valueResult };
        object actualValue = null;
        try
        {
            actualValue = Convert.ToDecimal(valueResult.AttemptedValue, CultureInfo.InvariantCulture);
        }
        catch (FormatException e)
        {
            modelState.Errors.Add(e);
        }

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

And in the global.ascx :

ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
阳光①夏 2024-11-09 17:37:31

尝试在 OnActionExecuting 中设置区域性。

顺便说一句,我发现了另一点。

public class CultureModelBinder : DefaultModelBinder
 {      
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = yourCulture;
        }
}

Try to set the culture in OnActionExecuting.

btw I found another point.

public class CultureModelBinder : DefaultModelBinder
 {      
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = yourCulture;
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文