C # -ModelState.IsValid only returns false when the resource language changes?

I have one box on my razor as shown below.

 @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 framework in MVC. The field duration is defined in the "Activity" database table (same model as on the shaver) as Float. But the object metadata shows it as Double as shown below.

  public Nullable<double> Duration { get; set; }

      

I have used a partial class like Activity.cs for the required checks 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 weird thing is that my code works well for float values ​​when my resource language for displaying labels is English and it doesn't work when I change it to another language (French) .here ModelState.IsValid returns false. And I am getting an error like

"The value 3.5(any float value) is invalid for Duration." 

      

how to fix this for another resource language ?. Any help would be appreciated

+3


source to share


2 answers


You are getting this error because your site culture is set to a language (French) that does not use a period .

as the decimal separator. However, the entry for Duration

has a dot in it, so your model state is evaluated as 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 synchronize the language of the client and server ON EACH REQUEST . Make sure your code to set up the culture is executed for every request, not just when the application starts. The user can switch languages ​​between requests. A setting CurrentCulture

to the appropriate language will use that numeric language formatting, date and time formatting, etc.

Also, it is recommended but not necessary to fix the problem, you must also install CurrentUICulture

, it will get shortcuts, messages, etc. from your language resource file (if you have resource files).

Follow @ orhun.begendi's answer above to set the above 2 items.

+2


source


You can easily override your global.asax.cs file like this

protected void Application_AcquireRequestState(object sender, EventArgs e)
{
     //Create culture info object 
     CultureInfo ci = new CultureInfo("en");

     if(Request.Url.Host.Equals("yourdomain.com",                                   StringComparison.InvariantCultureIgnoreCase))
     {
          ci = new CultureInfo("fr"); //in your case
     }

     System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
     System.Threading.Thread.CurrentThread.CurrentCulture =                                     CultureInfo.CreateSpecificCulture(ci.Name);
}

      



Also you can set begin_request method.

You can check this link for more information https://www.codeproject.com/articles/778040/beginners-tutorial-on-globalization-and-localizati

0


source







All Articles