使用Select TagHelper设置默认/空值 [英] Set Default/Null Value with Select TagHelper

查看:310
本文介绍了使用Select TagHelper设置默认/空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在asp.net mvc中,您可以使用:

In asp.net mvc you can use:

@Html.DropDownListFor(model => model.Category, ViewBag.Category as IEnumerable<SelectListItem>, "-- SELECT --", new { @class = "form-control" })

使用asp.net 5,如何在taghelper中包含默认值或空值(-SELECT-):

Using asp.net 5, how do I include the default or null value (-- SELECT --) in a taghelper:

<select asp-for="Category" asp-items="@ViewBag.Category"  class="form-control"></select>

推荐答案

您可以在选项中插入一个选项:

You can just insert an option item inside the select:

<select asp-for="Category" asp-items="@ViewBag.Category"  class="form-control">
    <option disabled selected>--- SELECT ---</option>
</select>

上面的代码中的disabled关键字表示,一旦在列表中选择了一个选项,就不能再次选择"--- SELECT-"行.如果您希望用户能够再次清空所选内容(即,如果将其绑定到可为空的字段),则只需禁用禁用即可.

The disabled keyword in the code above means that the "--- SELECT ---" row cannot be picked again once a choice has been selected in the list. If you want the user to be able to blank the selection again (i.e. if it's bound to a nullable field) then just omit disabled.

这篇关于使用Select TagHelper设置默认/空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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