简体   繁体   English

来自Javascript的HTML.Action包含js var作为参数

[英]Html.Action from Javascript include js var as parameter

I am having trouble on calling a methode. 我在调用方法时遇到麻烦。 I can do it with below codes but how to put in js var with my call? 我可以用下面的代码来做到这一点,但是如何在调用中放入js var?

            $.ajax({
                url: '@Url.Action("surfacepost", "surface", new { text = "ThisSchouldBeAJaVar" })',
                method: 'GET',
                success: function (data) {

                }
            });

In fact this mthode is getting a partial as I am going to display. 实际上,正如我要显示的那样,该方法正变得越来越不完整。 So if there is a better way of dynamic load a partial using js, please also let me know. 因此,如果有使用js动态加载部分内容的更好方法,也请让我知道。 Thanks 谢谢

The problem is that the '@Url.Action()' method is executed by .NET, before the page is send to the browser, it is not aware of any javascript on the page, and the execution of the method cannot be influenced by javascript that is run after the page has 'left the server' 问题是“ @ Url.Action()”方法由.NET执行,在将页面发送到浏览器之前,它不知道页面上有任何JavaScript,并且该方法的执行不受以下影响:页面“离开服务器”后运行的javascript

What you could do is create a placeholder, and replace it in javascript: 您可以做的是创建一个占位符,并将其替换为javascript:

var url = '@Url.Action("surfacepost", "surface", new { text = "placeholder" })'
url.replace('placeholder', ThisSchouldBeAJaVar);

$.ajax({   
url: url,
    method: 'GET',
    success: function (data) {

    }
});

Or you could just type the url and append the var: 或者,您可以只键入url并附加var:

$.ajax({   
url: '/surface/surfacepost/?text=' + ThisSchouldBeAJaVar,
    method: 'GET',
    success: function (data) {

    }
});

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

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