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.

Alan Araya
  • 701
  • 1
  • 12
  • 27
  • 1
    https://github.com/aspnet/Mvc/issues/2015 not supported yet, no plan either as far as I can see – MarkB May 13 '16 at 17:59

1 Answers1

2

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

    }
}
Joe Audette
  • 35,330
  • 11
  • 106
  • 99
  • Yeah, I found that way is the only one two. But instead of inheriting from Taghelper (based on my old helpers) I´am using an extension of HtmlHelper. What's the benefits of TagHelper? – Alan Araya May 15 '16 at 14:17
  • if you google for "benefits of taghelpers" you'll find good info. mainly it is about cleaner markup and separating markup from c# more cleanly vs HtmlHelpers. good example here http://www.davepaquette.com/archive/2015/05/11/cleaner-forms-using-tag-helpers-in-mvc6.aspx – Joe Audette May 15 '16 at 16:11