I am displaying a list of dropDownLists inside a ForEach
loop.
This is the code
@foreach (var task in Model.TaskList)
{
<tr>
<td>@Html.DropDownList("ModuleID", new SelectList(Model.ModuleList, "ModuleID", "Ordre"))
</td>
</tr>
}
I want the Module
of each Task
to be selected by default. This could easily be done using DropDownList
if only I was binding the dropDownList to a view model, but that's not the case.
Any ideas ?
The option tag contains the value that would be used when selected. The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.
A select box also called drop down box provides an option to list down various options in the form of drop down list. You can also preselect a value in dropdown list of items in HTML forms. For that, add selected in the <option> tag for the value you want to preselect.
Use ng-init to set default value for ng-options .
This could easily be done using DropDownList if only I was binding the dropDownList to a view model
You already know the correct answer and are still asking the question. And are still not using a view model.
What else can I say, you could pass the value of the element that you want preselected as 4th argument of the SelectList
constructor:
@Html.DropDownList(
"ModuleID",
new SelectList(Model.ModuleList, "ModuleID", "Ordre", task.ModuleID)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With