maztt
maztt

Reputation: 12294

merging ajax options in custom ajaxhelper

how can i merge the options in this custom extension method?

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

Upvotes: 0

Views: 475

Answers (2)

VJAI
VJAI

Reputation: 32768

You can merge the AjaxOptions to the link tag as,

linkTag.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes());

Additionally, you can merge the html attributes as,

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

Upvotes: 0

Steve Owen
Steve Owen

Reputation: 2071

AjaxOptions is just a class. You can set your own properties on it. I'd suggest using the existing Ajax helper and just changing the AjaxOptions first. So:

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

Upvotes: 1

Related Questions