I have an HTML dropdown list:
<select name="status">
<option value="close" >Close Task</option>
<option value="open" >Reopen Task</option>
</select>
I want to set the ‘selected’ option based on the ‘Task.Completion’ property in my view model:
public class TaskEditViewModel
{
public Task Task { get; set; }
public TaskComment TaskComment { get; set; }
}
So if Task.Completion is NULL, the ‘close’ option is selected, otherwise the ‘open’ option is selected.
How can I do this?
Your view model doesn’t seem adapted to what you are trying to do in the view (which according to your question is show a dropdown list and preselect a value based on some property on your view model).
So a far more realistic view model would be this:
then you could have a controller action which will populate and pass this view model to the view:
and finally inside your strongly typed view you could use the
DropDownListForhelper to render this dropdown: