Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make whole button link to the Ajax.ActionLink it contains

I am using the following code to achieve sorting:

<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 you might expect, when I click on the text inside the button it works, but if I click anywhere else (the padding between button boundary and text) it does not do anything.

Since there does not seem to be a Url. helper for ajax methods, and there is no Ajax.ButtonLink , I am a little bit lost on how to wrap the whole button in this ajax call.

like image 589
tacos_tacos_tacos Avatar asked Feb 13 '23 14:02

tacos_tacos_tacos


2 Answers

I was being thick. In this case (with Bootstrap classes) I just needed to add the button's classes, "btn btn-default" to my ActionLink's @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.
    )
like image 67
tacos_tacos_tacos Avatar answered Feb 23 '23 01:02

tacos_tacos_tacos


pull the ajax call out and tie it to the button click

$('.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);
        }


    });
});
like image 21
Matt Bodily Avatar answered Feb 23 '23 01:02

Matt Bodily