Alan Araya
Alan Araya

Reputation: 721

Where is "system.web.mvc.ajaxhelper" in asp.net core 1.0

I´m trying out the new asp.net 5 (core 1.0 now) on the "rc1-final" and I´m not finding the old class AjaxHelper that until Asp.Net 4 was at "system.web.mvc" dll.

Is there any nuget package containing this class? Or any other that substitute this one?

I´m using the "DNX 452" and do not pretend to move to "core 1.0"/"dnx 5" right now.

Upvotes: 0

Views: 1657

Answers (1)

Joe Audette
Joe Audette

Reputation: 36716

The mvc ajaxhelpers are mainly just setting up the data-ajax-* attributes that are used by jquery unobtrusive ajax. You can easily and more cleanly do that in ASP.NET Core just by adding the attributes in the markup, or if you want to be fancy about it you can implement taghelpers. For example in the older MVC 5 I used to use ajaxhelper to implement a custom bootstrap modal dialog. When I went to port that to ASP.NET Core I found like you that ajaxhelpers don't exist so I implemented a taghelper that adds the ajax attributes like this:

/// <summary>
/// this taghelper detects the bs-modal-link attribute and if found (value doesn't matter)
/// it decorates the link with the data-ajax- attributes needed to wire up the bootstrap modal
/// depends on jquery-ajax-unobtrusive and depends on cloudscribe-modaldialog-bootstrap.js
/// </summary>
[HtmlTargetElement("a", Attributes = BootstrapModalLinkAttributeName)]
public class BootstrapModalAnchorTagHelper : TagHelper
{
    private const string BootstrapModalLinkAttributeName = "bs-modal-link";

    public BootstrapModalAnchorTagHelper() : base()
    {

    }


    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        // we don't need to output this attribute it was only used for matching in razor
        TagHelperAttribute modalAttribute = null;
        output.Attributes.TryGetAttribute(BootstrapModalLinkAttributeName, out modalAttribute);
        if (modalAttribute != null) { output.Attributes.Remove(modalAttribute); }

        var dialogDivId = Guid.NewGuid().ToString();
        output.Attributes.Add("data-ajax", "true");
        output.Attributes.Add("data-ajax-begin", "prepareModalDialog('" + dialogDivId + "')");
        output.Attributes.Add("data-ajax-failure", "clearModalDialog('" + dialogDivId + "');alert('Ajax call failed')");
        output.Attributes.Add("data-ajax-method", "GET");
        output.Attributes.Add("data-ajax-mode", "replace");
        output.Attributes.Add("data-ajax-success", "openModalDialog('" + dialogDivId + "')");
        output.Attributes.Add("data-ajax-update", "#" + dialogDivId);

    }
}

Upvotes: 2

Related Questions