Click here to Skip to main content
15,890,527 members
Articles / Web Development / ASP.NET
Tip/Trick

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 108.5K   11   7
Enable/Disable control on a web page using MVC Razor view engine and Extension Methods.

Introduction

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.

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

Using the code

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

HTML
<div>
  @Html.TextBoxFor(m => m.UserName).DisableIf(()=> Model.IsEditMode==false)  
</div>  

The above code is more readable and easy to maintain.

The extensions method code is showed below:

C#
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;
}

License

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


Written By
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
Ratandeep Gupta24-Nov-15 6:43 
General5 Stars Pin
msgabbard28-Aug-15 3:56
msgabbard28-Aug-15 3:56 
Questionusing in code Pin
hubert noman5-Jun-14 3:00
hubert noman5-Jun-14 3:00 
GeneralGood job Pin
David Steverson22-Oct-13 6:17
professionalDavid Steverson22-Oct-13 6:17 
GeneralRe: Good job Pin
Massimiliano Peluso "WeDev Limited"22-Oct-13 8:17
Massimiliano Peluso "WeDev Limited"22-Oct-13 8:17 
GeneralMy vote of 2 Pin
SriramNidamanuri17-Oct-13 20:36
SriramNidamanuri17-Oct-13 20:36 
GeneralRe: My vote of 2 Pin
Massimiliano Peluso "WeDev Limited"18-Oct-13 0:11
Massimiliano 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.