MVC3 模型验证不适用于双精度
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最后,我关注了 Haacked 的这篇文章:
http://haacked .com/archive/2011/03/19/fixing-binding-to-decimals.aspx
它工作得很好。
这是代码:
在 global.ascx 中:
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 :
And in the global.ascx :
尝试在 OnActionExecuting 中设置区域性。
顺便说一句,我发现了另一点。
Try to set the culture in OnActionExecuting.
btw I found another point.