Skip to main content

Posts

Showing posts from 2012

Using Bootstrap with MVC Views

I’m a lazy developer by nature. That means I don’t like to do extra work when it isn’t necessary. Therefore, I am a big fan of MVC’s HTML helpers. One of my favorites is Html.EditorForModel().In case you haven’t heard about it, Bootstrap is a newish set of CSS and JavaScript from Twitter that makes designing responsive web sites pretty easy. The CSS has a modern and clean look, and pages based on it scale very well, even on mobile devices. The problem I ran into is that Bootstrap expects form elements (labels, inputs, etc.) to have specific classes so that it knows how to display them properly. But the EditorForModel helper doesn’t know about Bootstrap. Thus, I needed to find a way to substitute the default classes for Bootstrap’s.The first thing I tried was to use jQuery to change the classes dynamically.It worked, but I didn’t like having to rely on JavaScript for this. Short of rewriting all of the editing views and abandoning Html.EditorForModel, I thought that there must be a way…