如何阻止选择标记帮助程序自动添加“多个”属性 [英] How to stop select tag helper from automatically adding 'multiple' attribute

查看:117
本文介绍了如何阻止选择标记帮助程序自动添加“多个”属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .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>

它可以正确加载数据,但会自动向其中添加multiple属性。如何停止此操作,因为我需要在下拉菜单中显示此操作

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。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆