Inline helpers and page output - how?

流过昼夜 提交于 2019-12-11 14:02:29

问题


I have an HtmlHelper function that returns a MvcHtmlString and which I'd like to call in an inline helper like this:

@helper JsCss()
{
    Html.Script("jquery/jquery-1.6.2", cdn: true)
}

I call the inline helper from my page:

<head>
@JsCss()
</head>

...trouble is: nothing shows up on the page! it seems I have to do this:

@helper JsCss()
{
    <text>
    @Html.Script("jquery/jquery-1.6.2", cdn: true)
    </text>
}

so I guess the thing is I have to "print" the return value of my Html.Script call to the page... how else could I do this?


回答1:


A helper is a code block, you need to prefix the Html.Script with @ so Razor knows you want to output the return value (you don't need the <text></text>):

@helper JsCss()
{
    @Html.Script("jquery/jquery-1.6.2", cdn: true)
}


来源:https://stackoverflow.com/questions/9449728/inline-helpers-and-page-output-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!