I need to create the second overload of Razor helper and want to call one helper from another (with some specific parameters). Is there any way to implement it?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Sure:
using System.Web.Mvc;
using System.Web.Mvc.Html;
public static class ActionLinkExtensions
{
public static IHtmlString MyActionLink(this HtmlHelper html)
{
// call the base ActionLink helper:
return html.ActionLink("some text", "someAction");
}
}
and then in your view:
@Html.MyActionLink()
If you are talking about @helper
Razor helpers you need to pass an instance of the HtmlHelper
as argument because it is not available in the helper context:
@helper MyActionLink(HtmlHelper html)
{
@html.ActionLink("some text", "someAction")
}
and then:
@MyActionLink(Html)
Personally I prefer the first approach as it is view engine agnostic and can be ported across any other view engines you like whereas the second is Razor specific and if tomorrow Microsoft invent the Blade view engine you will have to rewrite much of your code.