简体   繁体   English

无法在 ajax 调用中访问 Url.Action 中的项目

[英]Unable to access item inside Url.Action in ajax call

I have a Ajax call and inside the success function i have a $.each loop.When i try to use dtl it shows me 'The Name dtl doesn't exist in the current context'我有一个 Ajax 调用,在成功函数中我有一个 $.each 循环。当我尝试使用 dtl 时,它显示我'当前上下文中不存在名称 dtl'

Code代码

$.ajax({
            url: $("#getAllNewsBasicDetails").val(),
            cache: false,
            type: "POST",
            success: function (_newsDetails) {

                $.each(newstype, function (i, dtl) {
                        content = '';

                        content += '<a href="#" class="text-black">' + dtl.NewsTitle + '</a>';
                        content += '<a class="btn btn-sm btn-primary" href="@Url.Action("NewsDetails", "News",new{@NewsId=dtl.NewsId })">'; // In here it shows 'The Name dtl doesn't exist in the current context'
                        content += '</div>';

                    $("#searchItems").prepend(content);
            });
        }

Please change your ajax action as follows:请按如下方式更改您的 ajax 操作:

$.ajax({
    url: $("#getAllNewsBasicDetails").val(),
    newsUrl: '@Url.Action("NewsDetails", "News",new{@NewsId=-100000 })',
    cache: false,
    type: "POST",
    success: function (_newsDetails) {

        $.each(newstype, function (i, dtl) {
                var newsUri = newsUrl;              
                content = '';
                content += '<a href="#" class="text-black">' + dtl.NewsTitle + '</a>';
                content += '<a class="btn btn-sm btn-primary" href="'+ newsUri.replace("-100000", dtl.NewsId)+'">';
                content += '</div>';

            $("#searchItems").prepend(content);
    });
}

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

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