Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom model binder for a property

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:

  1. 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) 
  2. To create a new type lets say MyCustomType: List<int> and register model binder for this type (this is an option)

  3. 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?

like image 947
rrejc Avatar asked Feb 02 '10 19:02

rrejc


People also ask

Can we create custom model binder?

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.

What is custom model binder in MVC?

Custom Model Binder provides a mechanism using which we can map the data from the request to our ASP.NET MVC Model.

How can we use a custom model binder in asp net core?

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.

What is model binder?

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.

What is the modelbinder attribute used for?

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.

What is the default model binder in MVC?

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.

What does the model binder do for nested objects?

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.

What is a custom model binder in Salesforce?

But custom model binder provides a way to bind our data which is in specific format to our model classes or action parameter.


1 Answers

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.

like image 112
queen3 Avatar answered Sep 29 '22 05:09

queen3