How to disable a DropDownListFor in MVC3 Razor?

18,425

Solution 1

like this, by specifying the HTML Attributes. DropDownListFor

@Html.DropDownListFor(
    x => x.ReminderTime,
    Model.RemindersList,
    new { disabled = "disabled" }
)

Solution 2

My solution thanks to Ravi hit

    @Html.DropDownListFor(x => x.ReminderTime,
                 new SelectList(Model.RemindersList, "Item1 ", "Item2"), new { disabled = "disabled" })
Share:
18,425
GibboK
Author by

GibboK

A professional and enthusiastic Senior Front End Developer. Listed as top 2 users by reputation in Czech Republic on Stack Overflow. Latest open source projects Animatelo - Porting to JavaScript Web Animations API of Animate.css (430+ stars on GitHub) Industrial UI - Simple, modular UI Components for Shop Floor Applications Frontend Boilerplate - An opinionated boilerplate which helps you build fast, robust, and adaptable single-page application in React Keyframes Tool - Command line tool which convert CSS Animations to JavaScript objects gibbok.coding📧gmail.com

Updated on June 23, 2022

Comments

  • GibboK
    GibboK almost 2 years

    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!

  • GibboK
    GibboK over 11 years
    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" })
  • Darren Griffith
    Darren Griffith over 10 years
    Please note that disabled form fields will not be submitted to the server.