Note that there are some explanatory texts on larger screens.

plurals
  1. PODecimal values with thousand separator in Asp.Net MVC
    primarykey
    data
    text
    <p>I have a custom modal class which contains a decimal member and a view to accept entry for this class. Everything worked well till I added javascripts to format the number inside input control. The format code format the inputted number with thousand separator ',' when focus blur.</p> <p>The problem is that the decimal value inside my modal class isn't bind/parsed well with thousand separator. ModelState.IsValid returns false when I tested it with "1,000.00" but it is valid for "100.00" without any changes.</p> <p>Could you share with me if you have any solution for this?</p> <p>Thanks in advance.</p> <p><strong>Sample Class</strong></p> <pre><code>public class Employee { public string Name { get; set; } public decimal Salary { get; set; } } </code></pre> <p><strong>Sample Controller</strong></p> <pre><code>public class EmployeeController : Controller { [AcceptVerbs(HttpVerbs.Get)] public ActionResult New() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult New(Employee e) { if (ModelState.IsValid) // &lt;-- It is retruning false for values with ',' { //Subsequence codes if entry is valid. // } return View(e); } } </code></pre> <p><strong>Sample View</strong></p> <pre><code>&lt;% using (Html.BeginForm()) { %&gt; Name: &lt;%= Html.TextBox("Name")%&gt;&lt;br /&gt; Salary: &lt;%= Html.TextBox("Salary")%&gt;&lt;br /&gt; &lt;button type="submit"&gt;Save&lt;/button&gt; &lt;% } %&gt; </code></pre> <hr> <p><em>I tried a workaround with Custom ModelBinder as Alexander suggested. The probelm solved. But the solution doesn't go well with IDataErrorInfo implementation. The Salary value become null when 0 is entered because of the validation. Any suggestion, please? Do Asp.Net MVC team members come to stackoverflow? Can I get a little help from you?</em></p> <p><strong><em>Updated Code with Custom Model Binder as Alexander suggested</em></strong></p> <p><strong>Model Binder</strong></p> <pre><code>public class MyModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (bindingContext == null) { throw new ArgumentNullException("bindingContext"); } ValueProviderResult valueResult; bindingContext.ValueProvider.TryGetValue(bindingContext.ModelName, out valueResult); if (valueResult != null) { if (bindingContext.ModelType == typeof(decimal)) { decimal decimalAttempt; decimalAttempt = Convert.ToDecimal(valueResult.AttemptedValue); return decimalAttempt; } } return null; } } </code></pre> <p><strong>Employee Class</strong></p> <pre><code> public class Employee : IDataErrorInfo { public string Name { get; set; } public decimal Salary { get; set; } #region IDataErrorInfo Members public string this[string columnName] { get { switch (columnName) { case "Salary": if (Salary &lt;= 0) return "Invalid salary amount."; break; } return string.Empty; } } public string Error{ get { return string.Empty; } } #endregion } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload