[英]ASP.NET MVC tags in a javascript?
Hi, 嗨,
I need to extract a proper URL for a AJAX call and this is what I have added in my js file : 我需要为AJAX调用提取一个正确的URL,这是我在js文件中添加的内容:
var GetLocationByParentPath = '<%= Url.Content("~/Location/GetLocationsByParent") %>';
The ASP.NET MVC tag will however not run so now is the question, how do I fill the GetLocationByParentPath with the correct value? 但是,ASP.NET MVC标记不会运行,所以现在问题是,如何使用正确的值填充GetLocationByParentPath?
BestRegards 最好的祝福
You're problem is that you're trying to accomplish something that is not supported, you cannot use C# code inside js files. 你的问题是你正在尝试完成一些不受支持的东西,你不能在js文件中使用C#代码。
However, you can do it in your aspx files (or cshtml) and the js file can communicate with those, so you have 3 options: 但是,您可以在您的aspx文件(或cshtml)中执行此操作,并且js文件可以与这些文件进行通信,因此您有3个选项:
1 . 1。 Add a parameter to your function in your js file that accepts the url
在接受url的js文件中为函数添加一个参数
inside the js: 在js里面:
function yourfunction(url)
{
var GetLocationByParentPath = url;
}
inside your aspx: 在你的aspx里面:
<script>
yourfunction('<%= Url.Content("~/Location/GetLocationsByParent") %>');
</script>
2 . 2。 Add a global js variable that contains this url:
添加包含此url的全局js变量:
inside your aspx: 在你的aspx里面:
<script>
var getLocationsUrl = '<%= Url.Content("~/Location/GetLocationsByParent") %>'
yourfunction();
</script>
inside the js (make sure to define getLocationsUrl before your function runs): 在js中(确保在函数运行之前定义getLocationsUrl):
function yourfunction()
{
var GetLocationByParentPath = getLocationsUrl ;
}
3 . 3。 Use the full hard-coded url (bad for refactoring but simple solution:
使用完整的硬编码网址(不适合重构,但简单的解决方案:
inside the js (make sure to define getLocationsUrl before your function runs): 在js中(确保在函数运行之前定义getLocationsUrl):
var GetLocationByParentPath = '/Location/GetLocationsByParent';
Hope this helpes 希望这有助于此
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.