Custom Binder for an Internal Property Model

My model is like this

public class MyModel { string ID {get;set;} string Title {get;set;} MyOtherModel Meta {get;set;} } 

How to define a custom model binding for a type (MyOtherModel), so when the default binder binds MyModel, it calls its own model binding for the "Meta" property. I registered it in App start, for example:

  ModelBinders.Binders[typeof(MyOtherModel)] = new MyCustomBinder(); 

but it does not work. Any idea or any good article with additional information regarding model binding?

+7
asp.net-mvc
source share
2 answers

There is an article on collections that touches a little on the complex display type:

Collections and a bit about complex types

On the other hand, this article can give you some useful tips:

http://odetocode.com/Blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx

I suggest you use the model binding for the MyModel class as a workaround, this is not an ideal solution, but you can refactor easily as soon as you find the best solution. :)

+1
source share

Actually, if you put something like this in the Edit / Create View with the model of your MyModel class:

 <%= Html.TextBox("Meta.Prop1") %> 

where Prop1 is a property of your MyOtherModel class, then

 UpdateModel(model); 

will populate the text box value for your custom submodel property. And vice versa, the value of the text field will also be filled with this submodel value.

So, as soon as you update the current model, you also update the submodels.

0
source share

All Articles