How to disable a DropDownListFor in MVC3 Razor?
Asked Answered
G

2

8

I have this working peace of code, which displays populate a DropDownList in my MVC3 Razor Web App.

       @Html.DropDownListFor(x => x.ReminderTime,
            new SelectList(Model.RemindersList, "Item1 ", "Item2"))

I would need keep the DropDownList populated but DISABLED it, so a User cannot select a value in the list.

Could you point me out in the right direction? Please provide me a sample of code. Thanks!

Gurdwara answered 22/1, 2013 at 9:20 Comment(0)
G
19

like this, by specifying the HTML Attributes. DropDownListFor

@Html.DropDownListFor(
    x => x.ReminderTime,
    Model.RemindersList,
    new { disabled = "disabled" }
)
Geralyngeraniaceous answered 22/1, 2013 at 9:23 Comment(2)
Thanks for your hit the wokring code in my specific case was @Html.DropDownListFor(x => x.ReminderTime, new SelectList(Model.RemindersList, "Item1 ", "Item2"), new { disabled = "disabled" })Gurdwara
Please note that disabled form fields will not be submitted to the server.Duaneduarchy
G
1

My solution thanks to Ravi hit

    @Html.DropDownListFor(x => x.ReminderTime,
                 new SelectList(Model.RemindersList, "Item1 ", "Item2"), new { disabled = "disabled" })
Gurdwara answered 22/1, 2013 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.