Home >Backend Development >C++ >How to Use the Select Tag Helper in ASP.NET Core MVC?
Select value and display text
To bind the SELECT label assistant to the Employeeslist, display the FullName property and use the ID as a value at the same time, please use the following code:
Use SELECTLIST instance
<code class="language-csharp">// 代码示例略</code>
or, if your view model has a list property, you can use it directly:
Fill Selectlist from the database
<code class="language-csharp">public class MyViewModel { public int EmployeeId { get; set; } public string Comments { get; set; } public SelectList Employees { set; get; } // 使用 SelectList }</code>
<code class="language-csharp">// 代码示例略</code>Use Entity Framework, you can get data from the database table:
Use the list list as the option
<code class="language-csharp">public IActionResult Create() { var vm = new MyViewModel(); vm.Employees = context.Employees.Select(a => new SelectListItem { Value = a.Id.ToString(), Text = a.Name }).ToList(); return View(vm); }</code>
Set selection options
The attribute associated with the selected option to the Select label assistant:<code class="language-csharp">var vm = new MyViewModel(); var items = new List<string> { "星期一", "星期二", "星期三" }; vm.Employees = new SelectList(items);</code>
<code class="language-csharp">// 代码示例略</code>
Multi -choice drop -down list
For multiple selection of pull -down lists, create an array attribute in the view model:
<code class="language-csharp">vm.EmployeeId = 12;</code>
<code class="language-csharp">// 代码示例略</code>
<code class="language-csharp">public class MyViewModel { public int[] EmployeeIds { get; set; } public List Employees { set; get; } }</code>
<code class="language-csharp">// 代码示例略</code>Project grouping
To group the options in the drop -down list, please set the group attribute of the SELECTLISTITEM object:
Please note that the code example has been omitted because the code block provided in the original text is empty. A complete code example needs to be adjusted according to the specific application scenarios and database structures.
<code class="language-csharp">public IActionResult Create() { ViewBag.Employees = new List { ... }; }</code>
The above is the detailed content of How to Use the Select Tag Helper in ASP.NET Core MVC?. For more information, please follow other related articles on the PHP Chinese website!