Razor Editor Templates

// Published September 12, 2013 by Swati

Being the solo developer, I inevitably had to write front-end code. Razor is beautiful — especially in how it generates forms. Forms are incredibly tedious to develop; there is a reason there are entire companies built around doing forms. But with Razor, once you’ve got a View Model defined, forms are as easy as:

And that’s it for the “HTML.” That’ll spit out the appropriate input field for each property in the view model (AFancyModel). The problem, of course, arises when you want to customize small things — such as showing an asterisk next to a required field, or adding your own CSS markup, especially if you’re using bootstrap. Microsoft uses its own markup for validation, etc., which well, isn’t the same as bootstrap. There are many libraries designed to bring together .NET MVC and Bootstrap together, but I felt it was overkill. All I want are editor files that change Microsoft’s markup to bootstrap’s markup. Why? Then when I decide to stop using bootstrap, I am not going around changing my code in a dozen or so places.

So what’s wrong then? Documentation on editor templates and how to use them is so sparse. I found only one, which is great, but I would have loved to see more examples.