ASP.NET MVC Model Binder for Generic Type

Create a modelbinder, override BindModel, check the type and do what you need to do public class MyModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (HasGenericTypeBase(bindingContext.ModelType, typeof(MyType<>)) { // do your thing } return base.BindModel(controllerContext, bindingContext); } } Set your model binder to the default in the global.asax protected void … Read more

Steve Sanderson’s BeginCollectionItem helper won’t bind correctly

Ok I think I see what is going on here. In the second sample, where you did the foreach, it looks like your cshtml was something like this (@ symbols may be incorrect): foreach (var war in Model.WarrantyFeaturesVm) { using (Html.BeginCollectionItem(“WarrantyFeaturesVm”)) { Html.HiddenFor(m => war.FeatureId) <span>@Html.DisplayFor(m => war.Name)</span> Html.HiddenFor(m => war.HasFeature) } } Because BeginCollectionItem … Read more

Inject a dependency into a custom model binder and using InRequestScope using Ninject

The ModelBinders are reused by MVC for multiple requests. This means they have a longer lifecycle than request scope and therefore aren’t allowed to depend on objects with the shorter request scope life cycle. Use a Factory instead to create the IPostRepository for every execution of BindModel

ASP.NET MVC 4 JSON Binding to the View Model – Nested object error

You can keep your existing ActionMethod untouched without the need of json serializing: In the client side create an object from your json: JSON.parse(jsonData) and send that in the $.ajax data property. Or, instead of creating json, create an object: var dataObject = new Object(); dataObject.Town = $(‘#txt-Town’).val(); dataObject.District = $(‘#txt-District’).val(); … And again, send … Read more

Changing the parameter name Web Api model binding

You can use the Name property of the FromUri binding attribute to use query string parameters with different names to the method arguments. If you pass simple parameters rather than your QueryParameters type, you can bind the values like this: /api/values/5?cap=somecap&id=1 public IHttpActionResult GetValue([FromUri(Name = “cap”)] string capabilities, int id) { }

MVC 4 ignores DefaultModelBinder.ResourceClassKey

This is not specific to ASP.NET MVC 4. It was the same in ASP.NET MVC 3. You cannot set the required message using DefaultModelBinder.ResourceClassKey, only the PropertyValueInvalid. One way to achieve what you are looking for is to define a custom RequiredAttributeAdapter: public class MyRequiredAttributeAdapter : RequiredAttributeAdapter { public MyRequiredAttributeAdapter( ModelMetadata metadata, ControllerContext context, RequiredAttribute … Read more

Model binding comma separated query string parameter

Here’s my improved version of Nathan Taylor’s solution used in archil’s answer. Nathan’s binder could only bind sub-properties of complex models, while mine can also bind individual controller arguments. My binder also gives you correct handling of empty parameters, by returning an actual empty instance of your array or IEnumerable. To wire this up, you … Read more

How to use a DI / IoC container with the model binder in ASP.NET MVC 2+?

A couple of observations: Don’t inject dependencies just to query them in the constructor There’s no reason to inject an ITimeProvider into a user just to invoke Now immediately. Just inject the creation time directly instead: public User(DateTime creationTime) { this.CreationTime = creationTime; } A really good rule of thumb related to DI is that … Read more