Search code examples
htmlasp.net-mvc-3razor

How to disable a button more elegantly


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?


Solution

  • 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" 
            } }
    />