Click here to Skip to main content
14,929,655 members
Articles / Web Development / ASP.NET
Posted 17 Oct 2013

Tagged as


11 bookmarked

Enable/Disable controls on a web page using MVC Razor view engine and Extension methods

Rate me:
Please Sign up or sign in to vote.
4.67/5 (11 votes)
17 Oct 2013CPOL
Enable/Disable control on a web page using MVC Razor view engine and Extension Methods.


When developing using ASP.NET MVC razor view engine there is sometime the need of disabling/enabling controls if a certain condition is verified.

The common example is when we want to disable a textbox if, let's say, a Boolean property on the Viewmodel is True.

In the below example we got a registration form and we want to disable the user name Textbox when the IsEditMode property is false.

What we should do is checking IsEditMode and if it is true disable the Textbox.

    @Html.LabelFor(m => m.UserName)
    @if (Model.IsEditMode==false)
        @Html.TextBoxFor(m => m.UserName,new{disabled="disabled"})
        @Html.TextBoxFor(m => m.UserName)

Using the code

With the extension method described in this article will be possible to achieve the same behavior using the code below:

  @Html.TextBoxFor(m => m.UserName).DisableIf(()=> Model.IsEditMode==false)  

The above code is more readable and easy to maintain.

The extensions method code is showed below:

public static class DisableHtmlControlExtension
    public static MvcHtmlString DisableIf(this MvcHtmlString htmlString, Func<bool> expression)
      if (expression.Invoke())
        var html = htmlString.ToString();
        const string disabled = "\"disabled\"";
        html = html.Insert(html.IndexOf(">", 
          StringComparison.Ordinal), " disabled= " + disabled);
        return new MvcHtmlString(html);
     return htmlString;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Massimiliano Peluso "WeDev Limited"
Architect PeluSoft Limited
United Kingdom United Kingdom
I have been fascinated by software development since I was 10 years old. I'm proud of learned the first programming language with an Olivetti PC 128S based on Microsoft BASIC. I'm a Software Architect/Senior Developer with a strong background of all the developing Microsoft's technologies.

Comments and Discussions

QuestionHow to remove it from ModelState for a required property Pin
Ratandeep Gupta24-Nov-15 6:43
MemberRatandeep Gupta24-Nov-15 6:43 
General5 Stars Pin
msgabbard28-Aug-15 3:56
Membermsgabbard28-Aug-15 3:56 
Questionusing in code Pin
hubert noman5-Jun-14 3:00
Memberhubert noman5-Jun-14 3:00 
GeneralGood job Pin
David Steverson22-Oct-13 6:17
professionalDavid Steverson22-Oct-13 6:17 
Thank you for the concise example, it works well.
GeneralRe: Good job Pin
Massimiliano Peluso "WeDev Limited"22-Oct-13 8:17
MemberMassimiliano Peluso "WeDev Limited"22-Oct-13 8:17 
GeneralMy vote of 2 Pin
SriramNidamanuri17-Oct-13 20:36
MemberSriramNidamanuri17-Oct-13 20:36 
GeneralRe: My vote of 2 Pin
Massimiliano Peluso "WeDev Limited"18-Oct-13 0:11
MemberMassimiliano Peluso "WeDev Limited"18-Oct-13 0:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.