Overview
SELECT TAG Helper provides a simple way to create elements (drop -down list) in the ASP.NET Core MVC view using model data.
Binded to the option set <select>
Assume that the view model contains a attribute and a list of employees stored in attributes:
EmployeeId
In the view, you can use Select Tag Helper to bind the options to EmployeesList
collection:
<code class="language-csharp">public class MyViewModel { public int EmployeeId { get; set; } public List<Employee> EmployeesList { get; set; } }</code>
The option list displayed in the drop -down list of the attribute. EmployeesList
The method is used to sort the list according to the attribute.
<code class="language-html"><select asp-for="EmployeeId" asp-items="@Model.EmployeesList.OrderBy(e => e.FullName)"></select></code>
asp-items
OrderBy
The attribute specification will be selected from the drop -down list to the attributes in the view model. In this example, it is : FullName
When submitting the form, the selected value will be automatically bound to the property of the view model.
asp-for
EmployeeId
If your view model has
<code class="language-html"><select asp-for="EmployeeId" asp-items="@Model.EmployeesList.OrderBy(e => e.FullName)"></select></code>
EmployeeId
More options
Multi -choice: List
uses the array type for the asp-items
attribute to enable multiple choices.
<code class="language-csharp">public class MyViewModel { public int EmployeeId { get; set; } public List<Employee> Employees { get; set; } }</code>
<code class="language-html"><select asp-for="EmployeeId" asp-items="@Model.Employees"></select></code>
attribute of each to group the options in the drop -down list.
ViewBag:asp-for
The above is the detailed content of How Can I Use the Select Tag Helper in ASP.NET Core MVC to Create Dropdown Lists?. For more information, please follow other related articles on the PHP Chinese website!