"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
public class MyBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
string key = bindingContext.ModelName;
var v = ((string[])bindingContext.ValueProvider.GetValue(key).RawValue)[0];
int outPut;
if (int.TryParse(v, NumberStyles.AllowThousands, new CultureInfo("en-US"), out outPut))
return outPut;
return base.BindModel(controllerContext, bindingContext);
}
}
ModelBinders.Binders.Add(typeof(int), new MyBinder());
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Marked as answer by erik001 on Feb 10, 2011 11:37 AM
Do you know by any chance if the default behaviour (not accepting thousandseparator) is a change from MVC 2 to MVC 3?
I think it is also not working in MVC 2.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
You shouldn't need to go to all that work. See my note on locale's and try to get client and server side validation working without a custom model binder. I'm interested it making this easier.
@Ricka6: I agree, I'd also rather not use a custom model binder.
In your "Note about locales" you refer to the decimal delimiter but not to the thousandsseparator.
And I only have an issue with the thousandsseparator.
The issue seems not to be related to the culture setting: no matter what culture I set, I get the server side error for both "1,000" and "1.000". (the decimal delimiter works fine)
If you have a better solution than adding a custom model binder, I would be glad to hear!
erik001
Member
2 Points
7 Posts
Server side validation does not accept thousandseparator
Feb 09, 2011 03:08 PM|LINK
Hello,
I'm using MVC 3 RTM, client side validation has been disabled.
I have a simple model with one integer field.
Server side validation does not accept numbers including one or more thousandseparators.
It shows: "The value '1,000' is not valid for QuantityInStock".
It does not matter what culture I use. Both "1.000" and "1,000" are never accepted.
Anybody any idea?
mvc 3 validation
raduenuca
All-Star
24675 Points
4250 Posts
Re: Server side validation does not accept thousandseparator
Feb 09, 2011 04:12 PM|LINK
add a globalization section to the Web.config file, and then set the uiculture and culture attributes, as shown in the following example:
Radu Enuca | Blog
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Server side validation does not accept thousandseparator
Feb 09, 2011 04:43 PM|LINK
See
http://haacked.com/archive/2010/05/10/globalizing-mvc-validation.aspx
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
erik001
Member
2 Points
7 Posts
Re: Server side validation does not accept thousandseparator
Feb 10, 2011 07:38 AM|LINK
Nope, this does not work.
Tried several globalization settings inlcuding setting the culture in Application_BeginRequest() in Global.asax.cs:
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
It does not make any difference.I don't think it's a culture related issue because both thousand separators do not work. It might be a globalization issue though.
erik001
Member
2 Points
7 Posts
Re: Server side validation does not accept thousandseparator
Feb 10, 2011 07:45 AM|LINK
@imran_ku_07
I've seen that page, it's about MVC 2. I am using MVC 3.
Tried the options, copied the example and recreated it for MVC 3 but no luck.
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Server side validation does not accept thousandseparator
Feb 10, 2011 10:10 AM|LINK
Create a Model Binder for int
public class MyBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
string key = bindingContext.ModelName;
var v = ((string[])bindingContext.ValueProvider.GetValue(key).RawValue)[0];
int outPut;
if (int.TryParse(v, NumberStyles.AllowThousands, new CultureInfo("en-US"), out outPut))
return outPut;
return base.BindModel(controllerContext, bindingContext);
}
}
ModelBinders.Binders.Add(typeof(int), new MyBinder());
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
erik001
Member
2 Points
7 Posts
Re: Server side validation does not accept thousandseparator
Feb 10, 2011 11:37 AM|LINK
Thanks! It works!
Do you know by any chance if the default behaviour (not accepting thousandseparator) is a change from MVC 2 to MVC 3?
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Server side validation does not accept thousandseparator
Feb 11, 2011 04:50 PM|LINK
I think it is also not working in MVC 2.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: Server side validation does not accept thousandseparator
Feb 12, 2011 02:49 AM|LINK
You shouldn't need to go to all that work. See my note on locale's and try to get client and server side validation working without a custom model binder. I'm interested it making this easier.
look at the end of my http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part6-cs
erik001
Member
2 Points
7 Posts
Re: Server side validation does not accept thousandseparator
Feb 12, 2011 04:02 PM|LINK
@Ricka6: I agree, I'd also rather not use a custom model binder.
In your "Note about locales" you refer to the decimal delimiter but not to the thousandsseparator.
And I only have an issue with the thousandsseparator.
The issue seems not to be related to the culture setting: no matter what culture I set, I get the server side error for both "1,000" and "1.000". (the decimal delimiter works fine)
If you have a better solution than adding a custom model binder, I would be glad to hear!