Mat*_*ics 26 string type-conversion razor razor-declarative-helpers asp.net-mvc-3
我有一个RazorHelpers.cshtml文件,app_code其中如下所示:
@using Molecular.AdidasCoach.Library.GlobalConstants
@helper Translate(string key)
{
@GlobalConfigs.GetTranslatedValue(key)
}
Run Code Online (Sandbox Code Playgroud)
但是,我有一个案例,我想将结果用作链接文本@Html.ActionLink(...).我无法将结果转换为字符串.
有没有办法从Razor帮助器返回普通字符串,以便我可以在HTML和@Html帮助器中使用它们?
Len*_*rri 16
在你的情况下,我认为这也有效:
@(GlobalConfigs.GetTranslatedValue(key))
Run Code Online (Sandbox Code Playgroud)
附加样本:
@helper GetTooltipContent()
{
if(Model.SubCategoryType == SUBCATTYPE.NUMBER_RANGE)
{
@(string.Format("{0} to {1}", Model.SubCategoryMinimum, Model.SubCategoryMaximum))
}
else if(Model.SubCategoryType == SUBCATTYPE.NUMBER_MAXIMUM)
{
@("<= " + Model.SubCategoryMaximum)
}
else if(Model.SubCategoryType == SUBCATTYPE.NUMBER_MINIMUM)
{
@(">= " + Model.SubCategoryMinimum)
}
}
Run Code Online (Sandbox Code Playgroud)
Spi*_*ynn 15
我认为没有办法让@helper其他类型的人回归HelperResult.但是你可以使用一个返回类型为的函数string,例如
@functions {
public static string tr(string key) {
return GlobalConfigs.GetTranslatedValue(key);
}
}
Run Code Online (Sandbox Code Playgroud)
然后
@Html.ActionLink(tr("KEY"), "action", "controller")
另见http://www.mikesdotnetting.com/article/173/the-difference-between-helpers-and-functions-in-webmatrix
编辑:MVC Razor:html.actionlink中的Helper结果表明你的助手可以使用返回一个字符串@Html.Raw(GlobalConfigs.GetTranslatedValue(key));