嵌入剃刀语法中的JavaScript变量 [英] embedding javascript variable within razor syntax

查看:122
本文介绍了嵌入剃刀语法中的JavaScript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的方法

I have a method that looks like this

function endcall_click(leadid) {
    document.location = '@Url.Action("index","dispo",new{id=leadid})/';
}

当然,这是行不通的,因为它把leadid作为服务器端的变量,但我想注入传递到方法的JavaScript变量。

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.

我试过,但没有奏效包装铅ID。

I tried wrapping lead id in but that didn't work.

function endcall_click(leadid) {
    document.location = '@Url.Action("index","dispo",new{id="<text>leadid</text>"})/';
}

任何想法?

推荐答案

您不能JavaScript变量注入到在服务器上进行评估的脚本仅仅是因为在目前这个脚本执行并产生输出这个变量也没有但前来存在。实现这一目标的唯一方法是操纵结果字符串:

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. The only way to achieve this is to manipulate the resulting string:

function endcall_click(leadid) {
    document.location = '@Url.Action("index", "dispo")/' + leadid;
}

其缺点是,这是假定操纵航线的JavaScript,如果你决定改变他们的code可能打破服务器上。

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变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆