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
// 代码示例略
or, if your view model has a list property, you can use it directly:
<数据> Fill Selectlist from the database
public class MyViewModel { public int EmployeeId { get; set; } public string Comments { get; set; } public SelectList Employees { set; get; } // 使用 SelectList }
// 代码示例略
<字> Use the list list as the option
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); }
<置> Set selection options
The attribute associated with the selected option to the Select label assistant:var vm = new MyViewModel(); var items = new List<string> { "星期一", "星期二", "星期三" }; vm.Employees = new SelectList(items);
// 代码示例略
<选> Multi -choice drop -down list
For multiple selection of pull -down lists, create an array attribute in the view model:
vm.EmployeeId = 12;
// 代码示例略
public class MyViewModel { public int[] EmployeeIds { get; set; } public List Employees { set; get; } }
// 代码示例略
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.
public IActionResult Create() { ViewBag.Employees = new List { ... }; }
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!