DalSoft / dalsoft-website-comments

0 stars 0 forks source link

blog/index.php/2010/05/21/mvc-model-binders/ #13

Open utterances-bot opened 2 months ago

utterances-bot commented 2 months ago

ASP.NET MVC Model Binders | DalSoft Ltd

How to use ASP.NET MVC Model Binders.

https://www.dalsoft.co.uk/blog/index.php/2010/05/21/mvc-model-binders/

DalSoft commented 2 months ago

Legacy Comments

Methods says: June 7, 2010 at 12:58 am good information, you write it very clean. I am very lucky to get this tips from you.


Carlos says: July 18, 2010 at 3:15 pm Beautiful code and explanation. Thanks for the hard work! 😉


Kyle Russell says: August 27, 2010 at 8:20 pm Could you post an example of the improvement you proposed by not using the magic string? I’m curious how that would work. Finding decent articles about things like model binders is not easy. Your posts have been extremely helpful. Thanks


RCM says: March 9, 2011 at 1:33 pm Great article. In the custom model binder, is there a way to re-validate the model after you update it with the List of Offices? Say your Offices model had DataAnnotations on it. Is there a way to validate the model and have it update the ModelState with any errors?


Andrew Knox says: March 12, 2011 at 3:03 pm Thank you. Finally a decent article on the basics model binding (well, I’d not come across any at least!), helped me out a lot 🙂


Scott says: April 11, 2011 at 5:49 pm Thanks for this – very concise. I’m stuck at an architectural challenge, that you touch on in your Best Practice section. In real life, the Offices are out in a database somewhere, so how do you go fetch the appropriate record in a ModelBinder. I agree that the ModelBinder should not be calling directly to the database, but the only other option I can see is to add all the Offices into the ViewBag (or similar) and iterate through them via the controllerContext. But that seems quite expensive, especially if you had 43,000 offices…


DalSoft says: April 11, 2011 at 10:32 pm You shouldn’t connect to the database via a model binder instead get the data from the database in the controllers action. The model binder is for binding the http request to your model, from the controllers action you then use the model values to query your database. If your worried about performance consider system.web.caching or output caching, if you have the choice go for output caching


Jarrett says: October 16, 2011 at 2:57 am I echo Andrew Knox here in saying finally, a decent post on how to easily extend the default model binder.


Mark says: June 20, 2012 at 10:10 pm It’s worth noting that you can also use the ModelBinder attribute on an controller’s action parameter, which is useful if the custom ModelBinder is required only for certain actions.