1

私が抱えている問題は、次の関数を呼び出すときに単に文字列を返すことです:

@{StringUtil.IfNullorEmptyOutputHyphen<decimal>(ViewBag.StockItem.Height);}

引数が null の可能性があるため、ここでは @{} が必要であり、したがって型制約が必要です。

@StringUtil.IfNullorEmptyOutputHyphen<decimal>(ViewBag.StockItem.Height)

上記の理由でエラーが発生します

<decimal>

したがって、 @{} 内で関数を呼び出す必要があります。

完全性のための関数は次のとおりです。

public static IHtmlString IfNullorEmptyOutputHyphen<T>(T? value) where T : struct
{
    if (value == null)
    {
        return new HtmlString("wtf99");
    }

    return new HtmlString(value.ToString());
}

要約すると、@ {}内で呼び出される関数から「文字列」を返すにはどうすればよいですか

4

3 に答える 3