Question

Is there a shorter version of the following:

Using ASP.NET MVC, this is in the HTML page

<%= IsTrue ? Html.Image("~/images/myimage.gif") : "" %>

I know I'm only really writing 3 extra characters, just wondering if there is something better.

Was it helpful?

Solution

No there is not, the ? operator is itself a short hand for the if else statement.

OTHER TIPS

It might be acceptable to create html helper:

public static string ImageIf(this HtmlHelper helper, condition, url){
    return condition ? helper.Image(url) : "";
}

usage:

<%= Html.ImageIf(IsTrue, "~/images/myimage.gif") %>

Not for the case you outlined.

If you are doing a null check on A you could write var b = A ?? string.Empty;

Kindness,

Dan

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top