I want to define this section only if some property (Model.ReadOnly
) is false
.
@section toolbar {
<div class="tool">
<div class="row">
@Html.ActionLink( Resources.Strings.Edit, "Edit", "Profile" )
</div>
<div class="row">
@Html.ActionLink( Resources.Strings.Delete, "Delete", "Profile" )
</div>
</div >
}
I tried wrapping it up in @if ( !Model.ReadOnly ) {}
but it doesn't work.
Is there a way to do this?
I do not want to define an empty section (as @itsmatt suggests), the layout of my page changes whether the section is defined or not (using IsSectionDefined( "toolbar" )
).
This works for me:
Essentially flipping where the conditional is. This essentially results in an empty section if
Model.ReadOnly
is true.Update:
So, what about moving that section to a
PartialView
and doing something like:in your View and then let the
MyAction
return you the appropriatePartialView
based on the ReadOnly value? Something like:Seems like that would work just fine.
Bertrand,
See:
Razor If/Else conditional operator syntax
basically (para-phrasing), ... Razor currently supports a subset of C# expressions without using @() and unfortunately, ternary operators are not part of that set.
also, this may be a way around the issue:
conditional logic in mvc view vs htmlhelper vs action
basically, use the if logic to call a partialview to satisfy your criteria.
[edit] this was my basic thinking (where your @section code is defined in that partial):
This should work.
I never said it would be pretty ;-)