I have on one of my views the following razor code:
@if (item.PMApproved != true) {
<button @(isEnabled ? null : "disabled")>Butt</button>
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
<input type="button" value="Reset" @{@((!item.PMApproved) ? null : new { disabled = "disabled" })}; />
No need for that bloated code, just keep it simple :-)
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>
try this
<button type="submit" disabled="@(!item.PMApproved)"></button>
Possible easy way:
<input type="button" @(item.PMApproved ? "disabled" : "") />