[EDIT] Ok, irony of ironies... Literally AS I was writing this, the MVC team at MS were putting out their latest version: MVC 2 RC 2. You can get it here (and yes, it fixes the problem I've described below): http://weblogs.asp.net/scottgu/archive/2010/02/05/asp-net-mvc-2-release-candidate-2-now-available.aspx
So, after some working with MVC 2 RC (Full title: ASP.NET Model-View-Controller Version 2, release candidate, or so I'm told ;) I discovered some lovely things about MVC. One of which, in particular, is the TryUpdateModel, or UpdateModel, method available in the Controller class. This is supposed to take a FormCollection from your Edit (et al) Action method (ie: response from a postback) and populate your model instance for you. It should do this by using an optional whitelist and blacklist of class property names and the names of the fields on your View's form, then by using reflection to assign those properties values.
You'll notice, of course, that the above tends towards the opportunity rather than the specific. This is because the only output from the TryUpdateModel I've had is an error stating "Value cannot be null or empty. Parameter name 'name'." Woo hoo.
Anyway, fortunately there's lots of help on this subject, below... and hopefully my own solution, later...
- Researching the problem: http://stackoverflow.com/questions/1936970/asp-net-mvc-2-problem-with-updatemodel
- The solution to the problem: http://www.blogs.victorero.com/2010/01/21/UsingTheASPNETMVC20RCSourceCodeToCorrectABug.aspx
- How to get the code to fix the problem: http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=37423
- Stacktrace post linking to the source: http://stackoverflow.com/questions/1936970/asp-net-mvc-2-problem-with-updatemodel
And so, in the end, I've had my first look at downloading the source for an open MS framework and there are lots of things to be learnt in reading other organisation's code. It's an interesting project and doesn't have to be long and painful. Just grabbing a download and browsing should throw up an idea or two and hopefully show the right or wrong way of approaching things. Or, at the very least, a different way.
Also, I just wanted to point out that these are a great pair of posts:
- And the blog post the link above links to: http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/