Reputation: 18630
I'm trying to disable a button based on a condition in my MVC app.
I came up with:
@if (!item.Validated && HttpContext.Current.User.IsInRole("regionaladmin")) {
<input type="button" class="btnvalidate" value="Validate" [email protected] /> <span>|</span>
}
else {
<input type="button" class="btnvalidate" value="Validate" [email protected] disabled = "disabled" /> <span>|</span>
}
But this seems rather rough.
Can anybody suggest a better way to make this less lines?
Upvotes: 2
Views: 5913
Reputation: 2744
This is not good, if you have 20 page only that's mean you will write this code in them, you should create of of the following:
An extension helper method that will write the needed HTML for you so it will be just as the following:
@Html.ButtonForWithRole(x=>"regionaladmin")
And encapsulate all your business logic in your extension method
Or make a partial view that will do the same and use the partial view but his little poor
Upvotes: 0
Reputation: 25521
You're using ASP.NET, no? Why not skip the inline code and do it in your .cs file?
Markup:
<input type="button" ID="validate" runat="server" class="btnvalidate" value="Validate" [email protected] />
Code behind:
if (!item.Validated && HttpContext.Current.User.IsInRole("regionaladmin")) {
validate.Attributes.Add("disabled","disabled");
}
Upvotes: 2
Reputation: 21262
You can use a ternary expression
<input type="button" class="btnvalidate" value="Validate" [email protected]
@( (!item.Validated && HttpContext.Current.User.IsInRole("regionaladmin")) ? "disabled" : "")
> <span>|</span>
Upvotes: 2