在 MVC3 Razor 视图引擎中使用 javascript 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11287484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:18:15  来源:igfitidea点击:

using javascript variable in MVC3 Razor view engine

javascriptasp.net-mvc-3

提问by Mohammed Faruk

Is it possible?

是否可以?

@Html.ActionLink( "Link text", "Action Name", new { id = *JAVASCRIPT VARIABLE* })

Please explain with example code.

请用示例代码解释。

采纳答案by Darko Z

No it is not possible, because .NET MVC 3 is server side code and is evaluated before being sent to the client, and javascript is Client side code which runs only once it is ON the client

不,这是不可能的,因为 .NET MVC 3 是服务器端代码,在发送到客户端之前会进行评估,而 javascript 是客户端代码,它只在客户端上运行一次

BUT the reverse is possible:

但反过来也是可能的:

var jsVar = "@yourMvcVar";

回答by krilovich

It actually is very much possible with the following:

实际上很有可能通过以下方式实现:

var hello = "Great Job!";

var text = "@Html.ActionLink( "Link text", "Action Name", new { id = "JSVar" })"
text = text.replace("JSVar",hello);

As easy as that! :)

就这么简单!:)

All the @Html.Action link does is create a url string so doing replace on it is the same as replace on any javascript string

@Html.Action 链接所做的只是创建一个 url 字符串,因此对其进行替换与对任何 javascript 字符串进行替换相同