Accept comma and dot as decimal separator [duplicate]

Cleanest way is to implement your own model binder public class DecimalModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); return valueProviderResult == null ? base.BindModel(controllerContext, bindingContext) : Convert.ToDecimal(valueProviderResult.AttemptedValue); // of course replace with your custom conversion logic } } And register it inside Application_Start(): ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder()); … Read more

How to use Json.NET for JSON modelbinding in an MVC5 project?

I’ve finally found an answer. Basically I don’t need the MediaTypeFormatter stuff, that’s not designed to be used in MVC environment, but in ASP.NET Web APIs, that’s why I do not see those references and namespaces (by the way, those are included in the Microsoft.AspNet.WeApi NuGet package). The solution is to use a custom value … Read more

MVC3 Non-Sequential Indices and DefaultModelBinder

I have this working, you have to remember to add a common indexing hidden input as explained in your referenced article: The hidden input with name = Items.Index is the key part <input type=”hidden” name=”Items.Index” value=”0″ /> <input type=”text” name=”Items[0].Name” value=”someValue1″ /> <input type=”hidden” name=”Items.Index” value=”1″ /> <input type=”text” name=”Items[1].Name” value=”someValue2″ /> <input type=”hidden” name=”Items.Index” … Read more

Custom DateTime model binder in Asp.net MVC

you can change the default model binder to use the user culture using IModelBinder public class DateTimeBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); bindingContext.ModelState.SetModelValue(bindingContext.ModelName, value); return value.ConvertTo(typeof(DateTime), CultureInfo.CurrentCulture); } } public class NullableDateTimeBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); … Read more