简体   繁体   中英

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. I just want to create a disabled dropdownlist. I have already created SelectList name "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 . you have to use one which takes htmlAttributes as a parameter.

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
)

See Details Here On MSDN

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})

by below code

@Html.DropDownList("statusList", String.Empty, new { @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" })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM