Url。操作错误不存在对象

时间:2022-11-30 22:57:08

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"].

Url。操作在结果[i][" classies_id "]上产生错误。

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

“结果”的名称在当前上下文中不存在

How do I transfer to my object result?

如何转移到对象结果?

1 个解决方案

#1


3  

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

不能将JavaScript(客户端)变量传递到Url。在服务器端处理的操作。

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

作为一种解决方案,您可以使用占位符来生成url。然后使用.replace()方法生成实际的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);
}

#1


3  

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

不能将JavaScript(客户端)变量传递到Url。在服务器端处理的操作。

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

作为一种解决方案,您可以使用占位符来生成url。然后使用.replace()方法生成实际的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);
}