I have the following controller action:
[HttpPost] public ViewResult DoSomething(MyModel model) { // do something return View(); }
Where MyModel
looks like this:
public class MyModel { public string PropertyA {get; set;} public IList<int> PropertyB {get; set;} }
So DefaultModelBinder should bind this without a problem. The only thing is that I want to use special/custom binder for binding PropertyB
and I also want to reuse this binder. So I thought that solution would be to put a ModelBinder attribute before the PropertyB which of course doesn't work (ModelBinder attribute is not allowed on a properties). I see two solutions:
To use action parameters on every single property instead of the whole model (which I wouldn't prefer as the model has a lot of properties) like this:
public ViewResult DoSomething(string propertyA, [ModelBinder(typeof(MyModelBinder))] propertyB)
To create a new type lets say MyCustomType: List<int>
and register model binder for this type (this is an option)
Maybe to create a binder for MyModel, override BindProperty
and if the property is "PropertyB"
bind the property with my custom binder. Is this possible?
Is there any other solution?
We can apply custom model binder using ModelBinder attribute by defining attributes on action method or model. If we are using this method (applying attribute on action method), we need to define this attribute on every action methods those want use this custom binding.
Custom Model Binder provides a mechanism using which we can map the data from the request to our ASP.NET MVC Model.
Model binding allows controller actions to work directly with model types (passed in as method arguments), rather than HTTP requests. Mapping between incoming request data and application models is handled by model binders.
Model binding is a well-designed bridge between the HTTP request and the C# action methods. It makes it easy for developers to work with data on forms (views), because POST and GET is automatically transferred into a data model you specify. ASP.NET MVC uses default binders to complete this behind the scene.
The ModelBinder attribute is used to register the custom model binder against the specific property that it should be used for: Now, when the application runs, this model binder will be used for the Week property in this instance.
The default model binder which is provided by ASP.NET Core MVC supports most of the common data types and would also meet most of our needs. The built-in model binding functionality can be extended by implementing a custom model binder which transform the input prior to binding it to a model.
The model binder should also resolve the simple property names in all nested objects even if there are same simple property names from different objects. Shown below is an example of a nesting object model for the searching and paging request, and the corresponding query string source data.
But custom model binder provides a way to bind our data which is in specific format to our model classes or action parameter.
override BindProperty and if the property is "PropertyB" bind the property with my custom binder
That's a good solution, though instead of checking "is PropertyB" you better check for your own custom attributes that define property-level binders, like
[PropertyBinder(typeof(PropertyBBinder))] public IList<int> PropertyB {get; set;}
You can see an example of BindProperty override here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With