tesicg tesicg - 3 years ago 149
Ajax Question

How to replace ActionLink with "a" tag in MVC 5?

The code looks as following:

1) Razor:

@Html.ActionLink("Edit", "Edit", new { id = item.Id })


But, I'd like to have something like this:

<a href="#" onclick="DisplayEditUser(@item.Id)">Edit</a>


2) JavaScript:

function DisplayEditUser(id) {
window.jQuery.ajax({
type: "GET",
url: '/UsersAdmin/Edit',
data: "id=" + id
}).done(function (partialViewResult) {
var index = window.Administration.GetActiveTabIndex();
if (index === 0) {
$('#UsersDiv').html(partialViewResult);
}
});
}


The item.Id is string. That what I've written didn't work.

How to pass item.Id to JavaScript function?

Answer Source

You have to pass an object to the data part of ajax.

function DisplayEditUser(id) {
    window.jQuery.ajax({
        type: "GET",
        url: '/UsersAdmin/Edit',
        data: {id: id},
    }).done(function (partialViewResult) {
        var index = window.Administration.GetActiveTabIndex();
        if (index === 0) {
            $('#UsersDiv').html(partialViewResult);
        }
    });
}

Also , you have to pass parameter like string.

<a href="#" onclick="DisplayEditUser('@item.Id')">Edit</a>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download