从另一个呼叫一个Razor助手 [英] Call one Razor helper from another

查看:41
本文介绍了从另一个呼叫一个Razor助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建Razor帮助器的第二个重载,并想从另一个(具有一些特定参数)调用一个帮助器.有什么办法可以实现吗?

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?

推荐答案

确定:

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");
    }
}

,然后在您看来:

@Html.MyActionLink()

如果您谈论的是 @helper Razor助手,则需要传递 HtmlHelper 的实例作为参数,因为它在助手上下文中不可用:

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")
}

然后:

@MyActionLink(Html)

我个人更喜欢第一种方法,因为它与视图引擎无关,并且可以移植到您喜欢的任何其他视图引擎上,而第二种方法是Razor特定的,如果明天微软发明了Blade视图引擎,则您将不得不重写很多代码

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.

这篇关于从另一个呼叫一个Razor助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆