[英]How to create dropdownlist Disabled in ASP.NET Razor MVC 5
<div class="form-group">
@Html.LabelFor(model => model.CourseStatus, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("statusList", String.Empty, new { @disbled = disabled})
@Html.ValidationMessageFor(model => model.CourseStatus)
</div>
</div>
I am new in ASP.NET MVC5 and RAZOR. 我是ASP.NET MVC5和RAZOR的新手。 I just want to create a disabled dropdownlist.
我只想创建一个禁用的下拉列表。 I have already created SelectList name "statusList".
我已经创建了SelectList名称“ statusList”。 Through which I am trying to call the status.
我试图通过它来称呼状态。 but I want to make it disbled and make one status as by default
但我想使其消失并默认设置为一种状态
Html.DropDownList
has these overloads . Html.DropDownList
具有这些重载 。 you have to use one which takes htmlAttributes
as a parameter. 您必须使用将
htmlAttributes
作为参数的参数。
you can use this overload: 您可以使用以下重载:
@Html.DropDownList("statusList", null, String.Empty, new { disabled = "disabled"})
you have to use following overload: 您必须使用以下重载:
Html.DropDownList(
string name,
IEnumerable<SelectListItem> selectList,
string optionLabel,
IDictionary<string, Object> htmlAttributes
)
You need to use like this.For an Empty drop down list with out any value you should use. 您需要这样使用。对于没有任何值的空下拉列表,您应该使用。
@Html.DropDownList("statusList", new List<SelectListItem> { }, String.Empty, new { disabled = "disabled" })
and if you want to populate values as well you can use like this. 如果您也想填充值,则可以这样使用。
@{
List<SelectListItem> list = new List<SelectListItem>();
list.Add(new SelectListItem { Value="1", Text="Test Status"});
}
@Html.DropDownList("statusList", list , String.Empty, new { disabled = "disabled" })
hi your problem its when you try to disabled ,i advice you to change this 您好您的问题,当您尝试禁用它,我建议您更改此
@Html.DropDownList("statusList", String.Empty, new { @disbled = disabled}) @ Html.DropDownList(“ statusList”,String.Empty,新的{@disbled = Disabled})
by below code 通过下面的代码
@Html.DropDownList("statusList", String.Empty, new { @disabled = true}) @ Html.DropDownList(“ statusList”,String.Empty,新的{@disabled = true})
Controller 控制者
IEnumerable<SelectListItem> itemEtatRenseig = listeEtatRens.Select(c => new SelectListItem
{
Value = c.EtatReseigne,
Text = c.EtatReseigne,
});
VewBag.EtatRenseign = new SelectList(itemEtatRenseig, "Value", "Text", Rapport.RapportList.FirstOrDefault().EtatReseigne);
View: 视图:
@Html.DropDownList("EtatRenseign",null,"--Sélectionner un etat--", new { disabled = "disabled" })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.