Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top