FernandoPaiva
FernandoPaiva

Reputation: 4460

How to create a dynamic @Html.ActionLink with JQuery?

I'm trying create a dynamic @Html.ActionLink with JQuery and I cannot do this.

How could I do this ?

trying

$('#tableView > tbody').empty();   
    if (data["CategoriaProduto"].length > 0) {
        $.each(data["CategoriaProduto"], function (i, cp) {
            var editLink = $('@Html.ActionLink("Edit", "edit", "CategoriaProduto"), new {id = ' + cp.id + '}');           

            $('#tableView > tbody:last-child').append('<tr>'
                                                    + '<td class="text-capitalize small">' + cp.descricao + '</td>'
                                                    + '<td class="col-md-1">' + editLink + '</td>'
                                                    + '</tr>');
        });
    }

Upvotes: 0

Views: 857

Answers (2)

K Scandrett
K Scandrett

Reputation: 16540

The short answer is you can't.

The command @Html.ActionLink() must be resolved on the server before the page is sent to the client's browser. jQuery will only operate in the client's browser.

What you can do however is render it in the browser and then copy the resulting html eg. <a href="/Edit/CategoriaProduto">My Link</a> or whatever the output turns out to be, and using that format in your loop.

Upvotes: 3

Nasim Bahar
Nasim Bahar

Reputation: 115

for each row you can specify and auto id, and these id pass as parameter to Controller. and this is dynamic url creation.

Upvotes: 0

Related Questions