How to tell Ajax.ActionLink OnSuccess callback which element initiated the ajax

后端 未结 1 807
南方客
南方客 2021-01-12 20:16

I want my razor view to look something like this

@Ajax.ActionLink(\"A\", \"Buy\", new AjaxOptions() { HttpMethod = \"Post\", OnSuccess = \"updateLetter\" },          


        
相关标签:
1条回答
  • 2021-01-12 20:55

    First fix your Ajax.ActionLink overload as yours won't compile.

    And to pass parameters you could do this:

    @Ajax.ActionLink(
        "A", 
        "About", 
        null,
        new AjaxOptions { 
            HttpMethod = "POST",
            OnSuccess = "updateLetter('A')" 
        }, 
        new { 
            id = "letter_A" 
        }
    )
    

    and then:

    function updateLetter(letter)
    {
        $("#letter-" + letter).toggleClass('selected');
    }
    

    Personally I am not a fan of the Ajax.* helpers. I use an alternative approach which consists of a standard HTML ActionLink:

    @Html.ActionLink(
        "A", 
        "About", 
        null,
        new { 
            @class = "letter"
            id = "letter_A" 
        }
    )
    

    which I unobtrusively AJAXify in a separate javascript file:

    $(function() {
        $('.letter').click(function() {
            var $letter = $(this);
            $.post(this.href, function(result) {
                $letter.toggleClass('selected');
            });
        });
    });
    
    0 讨论(0)
提交回复
热议问题