0

如何合并此自定义扩展方法中的选项?

  public static MvcHtmlString ActionLink(this AjaxHelper html,
                    string linkText,
                    string actionName,                        
                    object htmlAttributes,
                    AjaxOptions options )
    {
        RouteValueDictionary attributes = new RouteValueDictionary(htmlAttributes);


        TagBuilder linkTag = new TagBuilder("a");        

        UrlHelper url = new UrlHelper(html.ViewContext.RequestContext);

        linkTag.Attributes.Add("href", url.Action(actionName));



        return MvcHtmlString.Create(linkTag.ToString(TagRenderMode.Normal));
    }
}
4

2 回答 2

1

AjaxOptions 只是一个类。您可以在其上设置自己的属性。我建议使用现有的 Ajax 助手并首先更改 AjaxOptions。所以:

public static MvcHtmlString ActionLinkWithSpan(this AjaxHelper html,
                        string linkText,
                        string actionName,
                        object htmlAttributes,
                        AjaxOptions options)
{
    RouteValueDictionary attributes = new RouteValueDictionary(htmlAttributes);
    // Add more attributes here if you want

    options.InsertionMode = InsertionMode.InsertBefore; // As an example. Or amend any others here.

    return html.ActionLink(linkText, actionName, attributes, options);
}
于 2012-07-12T12:15:01.117 回答
0

您可以将AjaxOptions链接标签合并为,

linkTag.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes());

此外,您可以将 html 属性合并为,

linkTag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
于 2012-07-12T13:23:58.777 回答