0

Okay, so right now I'm trying to call an Ajax function from a normal text link, however I cannot get it to work properly(or at all). If I call it form a button, it will work fine.

Essentially I'm trying to reorder the items in a table.

how can I call Ajax from an ActionLink or Ajax?

Here is the script that works fine when called from a button:

<script type="text/javascript">

        $(document).ready(function() {

            $(".Name").click(function() {
                $.ajax({
                    url: '<%= Url.Action("MyAction") %>',
                    dataType: 'html',
                    success: function(result) { $("#tableToUpdate").html(result); }
                });
            });
        });
</script>

2 Answers 2

4

You need to cancel the default action of the anchor by returning false from the click handler;

$(".Name").click(function() {
    $.ajax({
        url: '<%= Url.Action("MyAction") %>',
        dataType: 'html',
        success: function(result) { 
            $("#tableToUpdate").html(result); 
        }
    });
    return false; // <-- that's important
});

But I would probably directly use the href of the link:

<%= Html.ActionLink("Foo", "MyAction", null, new { @class = "Name" }) %>

which I would unobtrusively AJAXify in a separate javascript file (progressive enhancement):

$(function() {
    $('.Name').click(function() {
        $('#tableToUpdate').load(this.href);
        return false;
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

@Tassadaque, no, that's bad from SEO perspective as it makes your site uncrawlerable. Progressive enhancement with javascript is a much better technique.
perfect, thanks! I didnt realize I had to alter the script! Will accept when I am allowed to(5 min. from now)
1

Have u looked at the ajax.actionlink

<%= Ajax.ActionLink("Update Status", "GetStatus", new AjaxOptions{UpdateTargetId="status" }) %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.