asp.net mvc - How do I use razor to make something a link conditionally? -
i have razor view i'm using code looks this:
@if(model.islink) { <a href="..."> } text needs appear @if(model.islink) { </a> }
this works code doesn't feel clean. there better/more accepted way of accomplishing pattern?
here simple method think little cleaner.
set text variable @ top of view:
@{ var sometext = "some text must appear"; }
then output conditionally:
@if (model.islink) { <a href='#'>@sometext </a> } else { @sometext }
the multi-line if
statement above avoids doing string construction html, if want condense syntax down 1 line can this.
@html.raw(model.islink?string.format("<a href='#'>{0}</a>",sometext):sometext)
Comments
Post a Comment