Blog

MVC 3 and Model Binding

By Carlos MartinezThe System.Web.Mvc namespace contains a class named DefaultModelBinder, the application object in MVC uses an instance of this class in order to binding all the values inside a post to a given model so it can be passed to an action. For normal scenarios no code is needed for these purposes.   When needed, a custom binder can be used in order to implement complex binding logic to a specific action or type. This new custom binder should implement the IModelBinder interface; this binder could be specified in the parameter definition of the post action for example:   [HttpPost] public ActionResult Index([ModelBinder(typeof(CustomModelBinder))] CustomModel model)   I...

How to implement custom validation logic using attributes in MVC3

By Carlos Martinez A common (and very recommended) practice when working with model validation in MVC3 is to decorate the model that will be sent to the view with validation attributes.  In order to do this MVC3 has several validation attributes ready to use for this scenarios such as: ·         DataTypeAttribute ·         RangeAttribute ·         RegularExpressionAttribute ·         RequiredAttribute An easy way to implement custom validation is to create attributes that inherits the ValidationAttribute  class and add logic to the IsValid Method. Adding code in this point will add server validations to the model (once the values are filled in the model throu a ModelBinder this vali...

HEAD.JS

By Daniel Ramirez A simple but yet powerful script, and a must have for all the fellow web developers out there.  As its slogan says: The only script in your <head>. But why is that?, what benefits does it have? Some of the best practices in web development is to execute and include scripts at the bottom of the page, and set all the styles on the <head> section. This is to improve the usability and render first all the visible parts to the user so he/she feels the site more responsive and fast. If scripts were to be put on the <head> section, it would then block the rendering of the page (since it’s single threaded) because it has to download, parse and execute these scr...