Pergunta

I have an Asp.net MVC application with a particular view that has a model with a float property.

My server locale says that , is decimal symbol and it works fine when I edit my model and enter for instance 7,5 in the text box and post it to server. Default model binder is able to bind this value as expected to seven and a half.

But when I display this same value using <%= this.Model.FloatValue %>, decimal symbol is converted to . which means that <%= %> obviously ignores server locale settings.

So. How do I solve this problem then? Which locale should be used? Server system locale which says that decimal symbol is , or browser locale setting that's set to en-gb, which means that . is decimal symbol.

Anyway. I just want this to work reliably.

Some code:

My controller actions:

public ActionResult Settings()
{
    Settings result = this.Service.GetActiveSettings();
    return View(result);
}

[HttpPost]
[HandleModelStateException]
public ActionResult Settings(Settings data)
{
    if (!this.ModelState.IsValid)
    {
        throw new ModelStateException(); // my custom exception which isn't important here
    }
    Settings result = this.Service.SaveSettings(data);
    return Json(result);
}

The second one is called asynchronously using $.ajax().

Relevant part of the partial view:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Settings>" %>
<div class="data">Float value: <strong><%= this.Model.FloatValue %></strong></data>
<div class="data">Integer value: <strong><%= this.Model.IntValue %></strong></data>
...

Any my model class:

/// <summary>
/// Represents application specific settings.
/// </summary>
public class Settings
{
    /// <summary>
    /// Gets or sets the integer value.
    /// </summary>
    /// <value>Integer value.</value>
    [Required(ErrorMessageResourceType = typeof(Resources.Settings), ErrorMessageResourceName = "QuotaRequired")]
    [Range(0, 365*24, ErrorMessageResourceType = typeof(Resources.Settings), ErrorMessageResourceName = "QuotaRange")]
    public int IntValue { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources.Settings), ErrorMessageResourceName = "WorkdayRequired")]
    [Range(.5, 24.0, ErrorMessageResourceType = typeof(Resources.Settings), ErrorMessageResourceName = "WorkdayRange")]
    public float FloatValue { get; set; }
}

As you may see, there's nothing unusual here. Oh and BTW: Range validation on FloatValue doesn't work either.

Foi útil?

Solução 2

Using custom CultureInfo when converting to string

In the end I used ToString() method and provide appropriate culture info. I had to provide a bit more code, but it works as expected:

<%= this.Model.FloatValue.ToString(new System.Globalization.CultureInfo("sl-SI") %>

And when I need to use this culture info multiple times on the same view I simply create a view variable and store it and then reuse it with every instance.

Outras dicas

Did you try disabling Client Based Culture? You can find it in web.config, system.web section.

<globalization enableClientBasedCulture="false"  />
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top