I have one field on my razor view as following
@Html.TextBoxFor(model => model.Duration, new { @class = "form-control txtSite input-large rounded-custom", name = "Duration", @type = "number", min = "0", id = "durationId", required = "true", Tabindex = "1", value = "0" })
@Html.ValidationMessageFor(model => model.Duration, "", new { style = "color: red" })
I used entity model structure in MVC .The field duration is defined in database table "Activity" (Same model i used on razor) as Float. But the entity metadata shows it as Double as follows.
public Nullable<double> Duration { get; set; }
I used partial class as Activity.cs for required validations as follows
[MetadataTypeAttribute(typeof(Activity.Metadata))]
public partial class Activity
{
internal sealed class Metadata
{
[Required(ErrorMessageResourceType = typeof(Resources.Common), ErrorMessageResourceName = "PleaseEnterDuration")]
public Nullable<double> Duration { get; set; }
}
}
on Controller my code is like this
[HttpPost]
public ActionResult AddActivity(Activity model)
{
if (ModelState.IsValid)
{
//Some Code
}
}
The strange is my code works well for float values when my Resource language to display labels is English and its not working when i change it into another language (french).here ModelState.IsValid returning false. And I am getting error as
"The value 3.5(any float value) is invalid for Duration."
how to fix it for another resource language?. Any help will be appreciated
You are getting that error because your site's culture is set to a language (French) that does not use a dot . as a decimal separator. However the entry for Duration has a dot in it so your model's state is evaluating to invalid.
In other words your site (server side) is in French culture but the browser or whatever client you are using is NOT in French.
Fix
You need to synch the language of the client and the server ON EVERY REQUEST: Make sure your code to set the culture is executed for each request and not just on application startup. The user can switch languages between requests. Setting CurrentCulture to the appropriate language will use that language's numeric formatting, datetime formatting etc.
Additionally, it is suggested but not required to fix your issue, you should also set the CurrentUICulture, it will get labels, messages etc. from your resource file for the language (If you have any resource files).
Follow @orhun.begendi answer above to set the above 2 items.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With