A common situation you can find in MVC, is to have an Enum in your model and want that rendered as a dropdown list in the browser.
If you are lucky enough to be using MVC5.1 (or higher), I’d recommend you look at this post that shows how to do this using the new Enum Support that’s part of MVC5.1. If not, then read on.
There are a number of ways of solving this, Currently on StackoverFlow there are 20 answers to this question, but none of them consider what I consider the more elegant solution of using templating.
For anyone not familiar with templating, I’d recommend reading Brad Wilson’s excellent blog on it. The following post is how I like to do it, using Asp.Net MVC with C# and the Razor view engine.
For a model that looks like this
With our Enum defined as:
Wouldn’t it be…
View original post 639 kelime daha