Currently I am using a Html.EditorFor control in a default 'Create' View page like this.
<%: Html.EditorFor(model => model.IsActive) %>
I would like to convert this to a drop down with values and still be binded to the model in the view. My question is two fold.
If there are only 2/3 values needed in the drop down..Is there a quick way to explicitly populate 2 or 3 values?
If the list is big and needs to come from a sql query, how to do this?
Thanks in advance for the help.
In order to generate a dropdownlist you need 2 properties on your view model: a scalar property to bind the selected value to and a collection property which will contain the items to show in the dropdown.
So you could define a view model:
and then on your main view model have a property of this type:
Now you could have a custom editor template for this type (
~/Views/Shared/EditorTemplates/DropDownListViewModel.ascx
):and then in your main view:
Now all that's left is to have your controller action rendering the main view to fill the
Foo
property with the corresponding values. The could be hardcoded, come from a repository or whatever. It doesn't really matter.On the other hand if you knew the values in advance you could hardcode them in the editor template (
~/Views/Shared/EditorTemplates/YesNoDropDown.ascx
):and then:
or by decorating the IsActive property on your view model:
and then: