I have a situation where I want to display a button as being enabled or disabled depending on a property which has been set on the view model.
@if (Model.CanBeDe
Try this:
Go ahead. Try it. You'll notice that when @Model.CanBeDeletedis false, the disable attribute is missing from the element. Conversely, when @Model.CanBeDeleted is true the disableelement is present, and is set to disable
How does it work?
It's thanks to Razor's "conditional attributes" feature. if you assign a razor variable to an atribute in your cshtml (or vbhtml) it will behave like this:
disabled=disabled, checked=checked... you get the idea)class="@myvar" => class="the_value_of_myvar")What I love about this sintax is that it greatly helps in keeping your razor views readable.
You can read more about it in this article