I'm trying to do something like the following
<div id="test"> @( string.IsNullOrEmpty(myString) ? @: : myString ) </div>
The above syntax is invalid, I've tried a bunch of different things but can't get it working.
Try the following:
@Html.Raw(string.IsNullOrEmpty(myString) ? " " : Html.Encode(myString))
But I would recommend you writing a helper that does this job so that you don't have to turn your views into spaghetti:
public static class HtmlExtensions { public static IHtmlString ValueOrSpace(this HtmlHelper html, string value) { if (string.IsNullOrEmpty(value)) { return new HtmlString(" "); } return new HtmlString(html.Encode(value)); } }
and then in your view simply:
@Html.ValueOrSpace(myString)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With