合并自定义ajaxhelper中的ajax选项

时间:2012-07-12 11:05:38

标签: asp.net-mvc asp.net-mvc-2

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

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

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

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

linkTag.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes());

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

linkTag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));