views:

479

answers:

2

I'm trying out sharp-architecture (and ASP.NET MVC) for a new project after being on the fringes of that community for a while and I'm a little confused. Following the standard entity template generation I created a class Message and I can list, create, edit and delete them.

I'm looking at the Edit(Message) action and just can't for the life of me figure out how it gets an input type of Message. You click on btnSave which submits the form to Messages/Edit?id=1 and then what happens? Is this a convention defined somewhere? If so where?

+2  A: 

I think that when you use a strongly typed view the framework automatically news up an object of the correct type for you and passes that to the controller action, by inspecting all the form inputs and using a bit of reflection to populate the corresponding properties.

Kirschstein
+3  A: 

The default model binder is doing the work for you. It reflects on the controller action, tries to new up types of the object in the args it found via reflection, then reads the formcollection and tries to do some parsing to match up the keys of the formcollection to appropriate values in the newed up object.

I say parsing because it is possible to represent objects that aren't entirely flat in the views, and the default model binder can often get them right. Although I haven't done this as I don't have a good use case for it, it may be possible to pass in multiple objects and have the model binder 'get' it. If not, it might be not too horrible to write one that could.

Chad Ruppert
So you're saying that the model binder binds form post fields back to a model is that correct? I assume then that it is possible to substitute your own?
George Mauer
Yes it sure is. Google "asp.net mvc default modelbinder" you register it in the global.asax
Chad Ruppert