ASP.NET 核心:标签助手 "select" 为空
ASP.NET Core : Tag helper "select" is empty
我需要一个包含 select 元素的视图。我有以下虚拟机 class:
public class EditTicketVM
{
public EditTicketVM()
{
Statuses = new SelectList(new List<SelectListItem>{
new SelectListItem("Open", "open"),
new SelectListItem("Pending", "pending"),
new SelectListItem("Hold", "hold"),
new SelectListItem("Solved", "solved"),
new SelectListItem("Closed", "closed"),
});
}
public string Description { get; set; }
public string Subject { get; set; }
public string Status { get; set; }
public SelectList Statuses { get; set; }
}
页面上的 和 select 元素:
<select asp-for="Status" class="form-control" asp-items="Model.Statuses" />
但是在结果页面上这个元素是空的:
我的代码有什么不正确的地方?
我已经在我这边测试了你的代码。问题出在您的 select
标签中。您正在使用自闭 select 标签。这是行不通的。按如下方式编写您的 select 标签:
<select asp-for="Status" class="form-control" asp-items="Model.Statuses">
<option value="">Select Status</option>
</select>
您的 SelectList
初始化也有问题。您初始化 SelectList
的方式将不起作用。按如下方式初始化您的 SelectList
:
public EditTicketVM()
{
Statuses = new SelectList(new List<SelectListItem> {
new SelectListItem(){Text = "Open", Value = "open"},
new SelectListItem(){Text = "Pending", Value = "pending"},
new SelectListItem(){Text = "Hold", Value = "hold"},
new SelectListItem(){Text = "Solved", Value = "solved"},
new SelectListItem(){Text = "Closed", Value = "closed"},
},"Value","Text");
}
我需要一个包含 select 元素的视图。我有以下虚拟机 class:
public class EditTicketVM
{
public EditTicketVM()
{
Statuses = new SelectList(new List<SelectListItem>{
new SelectListItem("Open", "open"),
new SelectListItem("Pending", "pending"),
new SelectListItem("Hold", "hold"),
new SelectListItem("Solved", "solved"),
new SelectListItem("Closed", "closed"),
});
}
public string Description { get; set; }
public string Subject { get; set; }
public string Status { get; set; }
public SelectList Statuses { get; set; }
}
页面上的 和 select 元素:
<select asp-for="Status" class="form-control" asp-items="Model.Statuses" />
但是在结果页面上这个元素是空的:
我的代码有什么不正确的地方?
我已经在我这边测试了你的代码。问题出在您的 select
标签中。您正在使用自闭 select 标签。这是行不通的。按如下方式编写您的 select 标签:
<select asp-for="Status" class="form-control" asp-items="Model.Statuses">
<option value="">Select Status</option>
</select>
您的 SelectList
初始化也有问题。您初始化 SelectList
的方式将不起作用。按如下方式初始化您的 SelectList
:
public EditTicketVM()
{
Statuses = new SelectList(new List<SelectListItem> {
new SelectListItem(){Text = "Open", Value = "open"},
new SelectListItem(){Text = "Pending", Value = "pending"},
new SelectListItem(){Text = "Hold", Value = "hold"},
new SelectListItem(){Text = "Solved", Value = "solved"},
new SelectListItem(){Text = "Closed", Value = "closed"},
},"Value","Text");
}