26

I have on one of my views the following razor code:

@if (item.PMApproved != true) {
                    <input type="button" class="btnresetinvoice button" value="Reset" data-invoiceid="@item.InvoiceId" />
                }
                else {
                    <input type="button" class="btnresetinvoice button" value="Reset" data-invoiceid="@item.InvoiceId" disabled="disabled" />
                }

Pretty rough. Basically I want to disable the button under a certain condition as you'd be able to work out from the code. What would be a more desirable way of doing this?

kapa
  • 75,446
  • 20
  • 155
  • 173
AnonyMouse
  • 17,278
  • 24
  • 77
  • 130

8 Answers8

20

A markup-centric solution aided by a new extension method:

public static class HtmlExtensions
{
   public static HtmlString DisabledIf(this HtmlHelper html, bool condition)
   {
      return new HtmlString(condition ? "disabled=\"disabled\"" : "");
   }
}

In your views, reuse out the wazoo:

<button type="reset" @Html.DisabledIf(someCondition)>Clear Fields</button>

Nicely reusable, and the rendered markup is very clean with regards to whitespace:

<button type="reset" disabled="disabled">Clear Fields</button>
David Grant
  • 3,426
  • 4
  • 28
  • 33
  • 1
    Great solution that really follows the spirit of the framework. Make sure you put the method in a namespace that MVC can see. – Daniel Feb 09 '18 at 00:07
19

I don't know what language you're using, but you might be able to move your if statement closer to the actual different between the two lines:

<input type="button" class="btnresetinvoice button" value="Reset"
       data-invoiceid="@item.InvoiceId"
       @{ if(item.PMApproved != true) { 
             @:disabled="disabled" 
        } }
/>
datoml
  • 5,164
  • 3
  • 19
  • 27
Brendan Long
  • 51,248
  • 19
  • 139
  • 179
  • Doesn't seem to be accepting the disabled part in the if statement. It says the name 'disabled' does not exist in the current context – AnonyMouse Apr 15 '12 at 21:40
  • 1
    @AnonyMouse - Fixed now thanks to Yogu. In the future, you should include the language in your question so people don't have to guess at the syntax. – Brendan Long Apr 15 '12 at 21:46
  • I'm trying to use this in my code but the `@:` seems to be hiding the `}` in my code which causes a syntax error any ideas on that? – Mykroft Jun 20 '13 at 14:48
  • @Mykroft Maybe try putting the plaintext on its own line? Or just the `` syntax instead? – Brendan Long Jun 20 '13 at 15:49
  • I ended up putting `this.write` instead. Not a big deal but you may want to recheck the code in your answer and make sure it works with the `/>` on the end there. – Mykroft Jun 20 '13 at 17:30
  • This didnt work for me (razor does not compile) - went with @David Grant solution below – wal Jun 18 '14 at 23:04
  • I was able to get this to work by using a syntax similar to the one found in [this answer](http://stackoverflow.com/a/9399772/744014). – Scott Sep 21 '15 at 20:05
  • I find code like this is ok until you need to add a 2nd, 3rd and 4th class conditionally, too. – Chad Aug 22 '16 at 20:48
18

Try this:

<button type="submit" disabled="@(!item.PMApproved)"></button>
Simone S.
  • 1,518
  • 15
  • 17
3

A helper could help:

public static class HtmlExtensions
{
    public static IHtmlString ApproveButton(this HtmlHelper htmlHelper, MyViewModel item)
    {
        var button = new TagBuilder("input");
        button.Attributes["type"] = "button";
        button.Attributes["value"] = "Reset";
        button.AddCssClass("btnresetinvoice");
        button.AddCssClass("button");
        button.Attributes["data-invoiceid"] = item.InvoiceId.ToString();
        if (item.PMApproved)
        {
            button.Attributes["disabled"] = "disabled";
        }
        return new HtmlString(button.ToString(TagRenderMode.SelfClosing));
    }
}

and then:

@Html.ApproveButton(item)
Darin Dimitrov
  • 994,864
  • 265
  • 3,241
  • 2,902
3

<input type="button" value="Reset" @{@((!item.PMApproved) ? null : new { disabled = "disabled" })}; />

No need for that bloated code, just keep it simple :-)

user1985065
  • 111
  • 1
  • 1
3
<button @(isEnabled ? null : "disabled")>Butt</button>
Oğuzhan Türk
  • 350
  • 2
  • 10
  • 20
3

Using asp.net mvc5 razor:

@if(condition)
{
   <button data-toggle="collapse" data-target="#content">Details</button>
}
else
{
   <button disabled>Details</button>
}

It prevents attempting to enable button from DevTools, because razor not visible for DevTools

Andrii
  • 629
  • 1
  • 5
  • 12
1

Possible easy way:

<input type="button" @(item.PMApproved ? "disabled" : "") />
Ire
  • 29
  • 3