0

我创建了一个 HtmlHelper 扩展调用 ActionButton,它将吐出一个 jQuery 按钮,该按钮将调用一个动作而不是使用 ActionLink。但是,我在下一行的扩展方法中收到了一个异常(它只是一个代码示例,会引发我看到的异常):

    MvcHtmlString str = helper.Action(actionName, controllerName, routeValues);

这是完整的方法,但使用 UrlHelper 创建操作 URL(对我有用)。我也有using System.Web.Mvcusing System.Web.Mvc.Html

    public static HtmlString ActionButton(this HtmlHelper helper, string linkText, string actionName, string controllerName, object routeValues)
    {
        TagBuilder tag = new TagBuilder("a");
        UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
        tag.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues));

        // the next line causes the exception, not really being used other than to 
        // raise the exception and illustrate the call I was making
        MvcHtmlString str = helper.Action(actionName, controllerName, routeValues);

        tag.MergeAttribute("rel", "external");
        tag.MergeAttribute("data-role", "button");
        tag.MergeAttribute("data-mini", "true");
        tag.MergeAttribute("data-inline", "true");
        tag.MergeAttribute("data-icon", "arrow-r");
        tag.MergeAttribute("data-iconpos", "right");
        tag.MergeAttribute("data-theme", "b");

        tag.SetInnerText(linkText);
        HtmlString html = new HtmlString(tag.ToString(TagRenderMode.Normal));
        return html;
    }

我想知道为什么调用helper.Action(...)会引发异常。

谢谢!

4

2 回答 2

0

这是因为你打电话helper.Action(那是HtmlHelper)而不是urlHelper.Action

于 2013-01-02T18:53:21.467 回答
0

HtmlHelper.Action()将立即调用您传递的操作方法,并返回其结果。
那不是你想要的。

You want UrlHelper.Action(),它返回操作的 URL。

于 2013-01-02T18:53:26.543 回答