Make an entire link to the Ajax.ActionLink link that contains

I use the following code to sort:

<button type="button" class="btn btn-default @(Model.SortOrder.Trim().ToUpper().Equals("NAME") ? "active" : "")">
    @Ajax.ActionLink("Name", "Cause", "Search", new { query = Model.Query, category = Model.Category, pageNumber = 0, sortOrder = "NAME", sortDirection = "ASCENDING" }, new AjaxOptions() { UpdateTargetId = "SearchCauseSelfWidgetContent", InsertionMode = InsertionMode.Replace, OnSuccess = "PostAjaxLoad()" })
</button>

As expected, when I click on the text inside the button, it works, but if I click elsewhere (indents between the border of the buttons and the text), it does nothing.

Since there is no Url.helper for ajax methods, and no Ajax.ButtonLink, I lost a bit how to wrap the entire button in this ajax call.

+4
source share
2 answers

I was thick. In this case (with Bootstrap classes) I just needed to add the classes button "btn btn-default"to mine ActionLink @class:

@Ajax.ActionLink(
    "Name",
    "Cause",
    "Search",
    new { query = Model.Query, category = Model.Category, pageNumber = 0, sortOrder = "NAME", sortDirection = "ASCENDING" },
    new AjaxOptions() { UpdateTargetId = "SearchCauseSelfWidgetContent", InsertionMode = InsertionMode.Replace, OnSuccess = "PostAjaxLoad()"},
    new { @class = "btn btn-default" } // < -- added.
    )
+7
source

ajax

$('.btn-default').on('click', function () {
    $.ajax({
        type: 'POST',
        url: '@Url.Action("Action", "Controller")',
        data: {
            query: '@Model.Query',
            category: '@Model.Category',
            etc...
        },
        cache: false,
        async: true,
        success: function (data) { 
            $('#SearchCauseSelfWidgetContent').html(data);
        }


    });
});
+1

Source: https://habr.com/ru/post/1530786/


All Articles