[英]embedding javascript variable within razor syntax
I have a method that looks like this 我有一个看起来像这样的方法
function endcall_click(leadid) {
document.location = '@Url.Action("index","dispo",new{id=leadid})/';
}
Of course it doesn't work because it treats "leadid" as a server side variable but I want to inject the javascript variable passed into the method. 当然它不起作用,因为它将“leadid”视为服务器端变量,但我想注入传递给方法的javascript变量。
I tried wrapping lead id in but that didn't work. 我试过包装引导ID但是没有用。
function endcall_click(leadid) {
document.location = '@Url.Action("index","dispo",new{id="<text>leadid</text>"})/';
}
Any ideas? 有任何想法吗?
You can't inject javascript variable to a script that is evaluated at the server simply because at the moment this script executes and generates the output this variable hasn't yet come to existence. 您不能将javascript变量注入到在服务器上评估的脚本,因为此脚本执行并生成输出时此变量尚未存在。 The only way to achieve this is to manipulate the resulting string:
实现此目的的唯一方法是操纵结果字符串:
function endcall_click(leadid) {
document.location = '@Url.Action("index", "dispo")/' + leadid;
}
The drawback is that this assumes manipulating the routes in javascript and if you decide to change them on the server the code might break. 缺点是这假设在javascript中操作路由,如果您决定在服务器上更改它们,代码可能会中断。
I finally found the solution (*.vbhtml): 我终于找到了解决方案(* .vbhtml):
<script type="text/javascript">
function razorsyntax() {
/* Double */
@(MvcHtmlString.Create("var szam =" & mydoublevariable & ";"))
alert(szam);
/* String */
var str = '@stringvariable';
alert(str);
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.