ASP.NET MVC – Alternative for [Bind(Exclude = “Id”)]

Yes there is: it’s called view models. View models are classes which are specifically tailored to the specific needs of a given view. So instead of: public ActionResult Index([Bind(Exclude = “Id”)] SomeDomainModel model) use: public ActionResult Index(SomeViewModel viewModel) where the view model contains only the properties which need to be bound. Then you could map … Read more

ASP.NET MVC posted file model binding when parameter is Model

It turns out the reason is that ValueProviderDictionary only looks in Request.Form, RouteData and Request.QueryString to populate the value provider dictionary in the model binding context. So there’s no way for a custom model binder to allow posted files to participate in model binding without inspecting the files collection in the request context directly. This … Read more

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

ASP.NET MVC – Mixing Custom and Default Model Binding

override the BindProperty from the DefaultModelBinder: public class CustomModelBinder:DefaultModelBinder { protected override void BindProperty( ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor ) { if (propertyDescriptor.PropertyType == typeof(Range)) { base.BindProperty(controllerContext, bindingContext, propertyDescriptor); } // bind the other properties here } }

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