Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 在@Helper声明中完成@Url Helper_Asp.net Mvc 3_Razor - Fatal编程技术网

Asp.net mvc 3 在@Helper声明中完成@Url Helper

Asp.net mvc 3 在@Helper声明中完成@Url Helper,asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,是否有办法访问@helper声明中的完整@Url帮助对象? 我想做这样的事 @helper Button(System.Web.Mvc.ActionResult action, string text) { <input type="button" name="btn" value="@text" onclick="windows.location='@Url.Action(action)'" /> } @helper按钮(System.Web.Mvc.ActionResu

是否有办法访问
@helper
声明中的完整
@Url
帮助对象?

我想做这样的事

@helper Button(System.Web.Mvc.ActionResult action, string text)
{
    <input type="button" name="btn" value="@text" onclick="windows.location='@Url.Action(action)'" />
}
@helper按钮(System.Web.Mvc.ActionResult操作,字符串文本)
{
}
我找到了,但此解决方案无法让您访问
@Url.Action(ActionResult)
。请注意我正在寻找的特定过载

有人想出了解决这个问题的办法吗?我总是可以为自定义helper使用扩展方法样式,但是我喜欢@helper样式,并且我已经多次遇到这个问题

提前谢谢大家

有没有办法在@helper声明中访问完整的@Url helper对象

是,将其作为参数传递给helper方法

@helper Button(string action, string text, UrlHelper url)
{
    <input type="button" name="btn" value="@text" onclick="windows.location='@url.Action(action)'" />
}

我不确定剃须刀,但是对于html助手,是的,你可以。使用助手的RequestContext,如下所示。下面是我为ImageFor html helper编写的一个示例:

    public static HtmlString ImageFor(this HtmlHelper helper, string url ) 
    {
        var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
        TagBuilder tb = new TagBuilder("img");
        tb.MergeAttribute("src", urlHelper.Content(url));
        return new HtmlString(tb.ToString(TagRenderMode.SelfClosing)); 
    }

好主意,但是不行,或者我做错了什么。我尝试了以下方法,得到的唯一的action方法重载是将action名称作为字符串的重载
@helper ActionButton(System.Web.Mvc.ActionResult action,string text,System.Web.Mvc.UrlHelper url){}
@jrizzo,操作方法的哪些重载不采用您正在讨论的字符串?我所知道的第一个参数是
System.Web.Mvc.ActionResult
,UrlHelper.Action方法没有重载。你一定是做错了什么。我正在寻找需要
ActionResult
@jrizzo的重载????????????恐怕你要找的超负荷根本不存在。看一下文档:嗯。。。我找不到文档,但是如果我在常规的.cshtml页面中键入
@Url.Action(myActionResult)
,Intellisense中的第一个重载将使用
ActionResult
。也许这是我的包裹的一部分。。。如果我知道是哪一个,我会给你回复(或者如果有人知道的话,请告诉我…)。
    public static HtmlString ImageFor(this HtmlHelper helper, string url ) 
    {
        var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
        TagBuilder tb = new TagBuilder("img");
        tb.MergeAttribute("src", urlHelper.Content(url));
        return new HtmlString(tb.ToString(TagRenderMode.SelfClosing)); 
    }