Monday, May 21, 2012

Conditionally wrapping content in Razor using

Using Razor I want to conditionally wrap some content in a <span> element based on a boolean property of my model. My guess is that I will need to use Templated Razor Delegates, but I'm finding it tricky to get them right.



My model goes something like:



public class Foo
{
public bool IsBar { get; set; }
}


and in my view I'd like to be able to use something similar to:



<a href="/baz">
@Html.WrapWith(Model.IsBar, "span", @This content may be wrapped, or not)
</a>


where it would render:



<!-- Model.IsBar == True -->
<a href="/baz">
<span>This content may be wrapped, or not</span>
</a>

<!-- Model.IsBar == False-->
<a href="/baz">
This content may be wrapped, or not
</a>




No comments:

Post a Comment