I have a view that renders a few drop downs on it. These are built from IDictionary’s.
When I render a view and I want to show the currently selected value of one of these options, I want to see how to convert the number in my view into the string value in the dictionary.
I got quite far with this (as my dictionary starts from 0) but it doesn’t quite work.
<%= Html.Encode(Chart.TypeDictionary.ElementAt(Model.iType)) %>
this returns this
[Public, 0]
Presumably I can just get the value on the left?
Does anyone have a better solution to this, and also what is the best way to do it when you don’t have drop downs.
I have used enums before, but obviously you can’t have spaces in them.
Does this not work?
Edit
Without more context, I can’t give you a definitive answer on what I’d consider to be the best way to go about this. When I’m displaying a dropdown list, I’ll often store the options in the view model in a form that will make it easy to create the dropdown. For example:
However, if you’re using the same model as an input value to your controller action, this won’t work. In that case, I’d store the options in the ViewData. If you’re representing an enum value type, one option is to create an extension method that takes the enum type in question, along with the current value, and creates a dropdown list for you. If you follow the CamelCase convention in your enum values, you can simply have this method put a space between lower-case and upper-case letters. (e.g. CamelCase => “Camel Case”).