如何在RazorView中禁用动态设置Html.DropDownList属性? [英] how to Dynamic setting Html.DropDownList attribute disabled in RazorView?

查看:97
本文介绍了如何在RazorView中禁用动态设置Html.DropDownList属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的下拉菜单:

@Html.DropDownList("OptionType", selectList, new { @class = "form-control", name = "OptionType",@disabled = "disabled" })

上面的代码可以将DropDownList设置为禁用,但是我想使用模型中的bool值来动态设置Disabled属性.换句话说,如果bool value = true,则启用DropDownList,否则禁用DropDownList.如何实现?

The above code can set DropDownList is disabled,but I want to dynamic set disabled attribute with a bool value from model. In other words if bool value = true, DropDownList is enable, else DropDownList is disabled. how to achieve it?

推荐答案

如果要基于模型的属性禁用下拉列表,则:

If you want to disable the dropdown based on a property of your model:

@if(Model.DisableDropdown)
{ 
    @Html.DropDownList("OptionType", selectList, new { @disabled = "disabled", @class = "form-control" })
}
else
{
    @Html.DropDownList("OptionType", selectList, new { @class = "form-control" })
}

这篇关于如何在RazorView中禁用动态设置Html.DropDownList属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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