从asp.net核心应用程序中删除多项选择 [英] Remove multiple selection from asp.net core application

查看:89
本文介绍了从asp.net核心应用程序中删除多项选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个值的List<SelectListItem>变量.我想将其表示为html中的下拉框,所以我正在这样做.

I have List<SelectListItem> variable with two values. I want to represent it as dropdown box in html so I'm doing like this.

<div class="form-group row">
    <label asp-for="Roles" class="col-sm-2 col-sm-offset-2 form-control-label"></label>
    <div class="col-md-6">
        <select asp-for="Roles" asp-items="@Model.Roles" class="form-control selectpicker bs-select-hidden"></select>
    </div>           
</div>

并且此代码向我显示了包含这两项的列表,但它还会生成

and this code shows me the list with those two items, but it also generates

multiple="multiple"

选择标签的

属性.

attribute for select tag.

我如何不生成多个属性?

How can I make not to generate multiple attribute?

推荐答案

如果您的asp-for属性是IEnumerable,则Select Tag Helper会自动将select元素设为多个.避免这种情况的方法是将基类(而不是集合)用作asp-for属性. asp-items属性仍然应该是一个集合,因为这些项将成为选择列表中的选项.

The Select Tag Helper automatically makes the select element multiple if your asp-for property is an IEnumerable. The way to avoid that is to use your base class (not a collection) as the asp-for property. The asp-items property should still be a collection since these are the items that will become options in the select list.

在您的示例中,这只是将您的asp-for =角色"更改为asp-for =角色"

In your example this is simply changing your asp-for="Roles" to asp-for="Role"

<div class="form-group row">
<label asp-for="Roles" class="col-sm-2 col-sm-offset-2 form-control-label"></label>
<div class="col-md-6">
    <select asp-for="Role" asp-items="@Model.Roles" class="form-control selectpicker bs-select-hidden"></select>
</div>           

您可能需要调整传递给视图的视图模型,以便它可以访问基类Role以及要枚举的Roles集合

You may need to adjust your view model being passed to the view so that it has access to the base class Role, as well as the collection of Roles to be enumerated

ASP NET Core选择标记帮助器参考

这篇关于从asp.net核心应用程序中删除多项选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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