开发者

MVC3 Model validation not working on a double

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 valid开发者_运维技巧ation 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!


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());


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;
        }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜