简体   繁体   English

如何使用@ url.Action使用JavaScript传递带有值的参数

[英]How to pass parameters with value using @url.Action using javascript

$.ajax({
    url: "/Home/GetAllSearchData",
    datatype: "json",
    type: "get",
    data: {
        text: Search,
        city: city,
        locality: locality
    },
    success: function(data) {
        if (data.result != null) {
            $("#table").find("tr:not(:first)").remove();
            var tr;
            for (var i = 0; i < data.result.length; i++) {
                var res = [];
                res = data.result[i].Id.split(",");
                tr = $('<tr/>');
                tr.append("<td>" + " <div class='dashboardmodal - body'><p><a href='@Url.Action("
                    SearchAction ", "
                    Home ", new { city = city })'>" + data.result[i].name + "</a></p>" + "</div></td>");
                $('#table').append(tr);
            }
            var modal = document.getElementById('myModal');
            modal.style.display = "block";
        }
    },
    error: function(err) {
        alert(err);
    }
});

I am trying to pass city variable value to anchor tag parameter but getting error the name does not exist in the current context 我正在尝试将城市变量值传递给锚标记参数,但是the name does not exist in the current context错误

Better change the object name city to cityObj and then try passing the same .. or just try the below one and check in console 最好将对象名称city更改为cityObj,然后尝试传递相同的..或仅尝试以下命令并检入控制台

'@Url.Action("SearchAction", "Home")?city=' + city +

or your could try the below, 或者您可以尝试以下方法,

'@Url.Action("SearchAction", "Home")', { city: city }

Hope it helps !! 希望能帮助到你 !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM