30

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
  • 77,694
  • 21
  • 158
  • 175
AnonyMouse
  • 18,108
  • 26
  • 79
  • 131

9 Answers9

25

Try this:

<button type="submit" disabled="@(!item.PMApproved)"></button>
Simone S.
  • 1,756
  • 17
  • 18
22

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,554
  • 4
  • 21
  • 28
Brendan Long
  • 53,280
  • 21
  • 146
  • 188
  • 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
  • 2
    @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
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,447
  • 4
  • 29
  • 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
5

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
  • 689
  • 1
  • 6
  • 12
4

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

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

user1985065
  • 121
  • 1
  • 1
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
  • 1,023,142
  • 271
  • 3,287
  • 2,928
3
<button @(isEnabled ? null : "disabled")>Butt</button>
Oğuzhan Türk
  • 360
  • 2
  • 9
  • 21
1

Possible easy way:

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

This will do it elegantly:

<button type="button"
        class="save"
        onclick="doSomething()"
        style="@(hasValidationErrors ? "background-color:gray;cursor:default" : string.Empty)" @(hasValidationErrors ? "disabled" : string.Empty)
        title="@validationErrors"
        @{
            if (ViewModel.Property == true)
            {
                @:disabled="disabled"
            }
         }>

Notice ViewModel.Property == true... this will add the disabled attribute to the button only if that view model property holds true. If false, no disabled attribute will be present and as such the button will be enabled.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480