1

以下是否有更短的版本:

使用 ASP.NET MVC,这是在 HTML 页面中

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

我知道我实际上只写了 3 个额外的字符,只是想知道是否有更好的东西。

4

3 回答 3

8

创建 html 助手可能是可以接受的:

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

用法:

<%= Html.ImageIf(IsTrue, "~/images/myimage.gif") %>
于 2009-09-15T11:14:03.103 回答
6

不,没有,?运算符本身就是 if else 语句的简写。

于 2009-09-15T11:07:49.497 回答
1

不适用于您概述的情况。

如果你正在做一个空检查,A你可以写var b = A ?? string.Empty;

善良,

于 2009-09-15T11:09:58.370 回答