Denisiva
Denisiva

Reputation: 23

Url.Action error dont exist object

I have Script

$(function() {
    $.support.cors = true;
    jQuery.support.cors = true;
    $.ajax({
        crossDomain: true,
        type: 'GET',
        url: 'http://example.com/WCFRESTService.svc/GetCategories',
        success: function(result) {
            var s = '';
            for (var i = 0; i < result.length; i++) {
                s += '<br><a href="@Url.Action("GetAnn", "Home",new { categories_id=result[i]["Categories_id"]})">' + result[i]["Name_Category"] + '</a>';
                $('#content').html(s);
            }
        }
    });
});

The Url.Action Gives an error on result[i]["Categories_id"].

The name "result" does not exist int the current context

How do I transfer to my object result?

Upvotes: 1

Views: 58

Answers (1)

Satpal
Satpal

Reputation: 133403

You can't pass JavaScript(Client Side) variable to Url.Action as it is processed at the Server-Side.

As a workaround, you can use placeholder to generate the url. Then use .replace() method to generate the actual url.

var s = '';
//Generate a variable with URL
var url = '@Url.Action("GetAnn", "Home", new { categories_id = -1})'; 
for (var i = 0; i < result.length; i++) {
    s += '<br><a href="' + url.replace(-1, result[i]["Categories_id"]) + '">' + result[i]["Name_Category"] + '</a>';
    $('#content').html(s);
}

Upvotes: 3

Related Questions