Click here to Skip to main content
14,210,773 members
Click here to Skip to main content
Tip/Trick
Posted 17 Oct 2013

Tagged as

Stats

80.8K views
11 bookmarked

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

,
Rate this:
4.67 (11 votes)
Please Sign up or sign in to vote.
4.67 (11 votes)
17 Oct 2013     CPOL    
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.

<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:

<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:

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)

Share

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 
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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190618.1 | Last Updated 17 Oct 2013
Article Copyright 2013 by Massimiliano Peluso "WeDev Limited"
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid