Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can create a Binder extension to handle the date in the culture format.</p> <p>This is a sample I wrote to handle the same problem with Decimal type, hope you get the idea</p> <pre><code> public class DecimalModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); ModelState modelState = new ModelState { Value = valueResult }; object actualValue = null; try { actualValue = Convert.ToDecimal(valueResult.AttemptedValue, CultureInfo.CurrentCulture); } catch (FormatException e) { modelState.Errors.Add(e); } bindingContext.ModelState.Add(bindingContext.ModelName, modelState); return actualValue; } } </code></pre> <p><strong>Update</strong></p> <p>To use it simply declare the binder in Global.asax like this</p> <pre><code>protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); //HERE you tell the framework how to handle decimal values ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder()); DependencyResolver.SetResolver(new ETAutofacDependencyResolver()); } </code></pre> <p>Then when the modelbinder has to do some work, it will know automatically what to do. For example, this is an action with a model containing some properties of type decimal. I simply do nothing</p> <pre><code>[HttpPost] public ActionResult Edit(int id, MyViewModel viewModel) { if (ModelState.IsValid) { try { var model = new MyDomainModelEntity(); model.DecimalValue = viewModel.DecimalValue; repository.Save(model); return RedirectToAction("Index"); } catch (RulesException ex) { ex.CopyTo(ModelState); } catch { ModelState.AddModelError("", "My generic error message"); } } return View(model); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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