如何阻止选择标签助手自动添加“多个"属性 [英] How to stop select tag helper from automatically adding 'multiple' attribute
问题描述
我正在使用 .Net Core VS2017
并尝试使用模型中的数据填充我的下拉列表.为此,我将我的选择列表声明为
and trying to populate my dropdown with data from the model. For this purpose I declared my select list as
<select class="form-control" asp-for="TicketTypes" asp-items="@(new SelectList(Model.TicketTypes,"Id","Name"))"></select>
它正在正确加载数据,但它会自动向其中添加多个属性.如何停止此操作,因为我需要在下拉列表中显示此内容
It is loading the data correctly but it automatically adds the multiple attribute to it. How to stop this because I need to show this in dropdown
推荐答案
因此,如果 asp-for 属性中指定的属性是 IEnumerable,那么 select 标记助手将自动生成多选.
So it turns out the select tag helper will automatically generate a multi-select if the property specified in the asp-for attribute is an IEnumerable.
public IEnumerable<Models.TicketTypeList> TicketTypes { get; set; }
所以我更改了代码并将数据存储在 ViewBag
中作为 SelectList
通过将控制器中的代码更改为
So I changed the code and stored the data in ViewBag
as SelectList
by changing the code in controller to
var ticketTypes = _context
.TicketType
.Where(w => w.Deleted == false)
.Select(s => new {
id = s.Id,
Name = s.Name
}).OrderBy(o=>o.Name).ToList();
ViewBag.ticketTypeList = new SelectList(ticketTypes, "id", "Name");
然后在视图中把代码改成
and then in the view changed the code to
@Html.DropDownList("TicketTypes",ViewBag.ticketTypeList as SelectList, new { @class = "form-control" })
这篇关于如何阻止选择标签助手自动添加“多个"属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!