[英]combine C# and javascript code
i have a javascript
alert in C# code like this 我在C#代码中有一个
javascript
警报,像这样
if(Session["msg"] != null){
string msg = (string)Session["msg"];
if(msg.Length > 2) {
@: var msg = @msg;
@: alert(msg);
}
But in the view the alert doesn't appear: 但是在视图中没有出现警报:
The problem is that the alert message is written to the view. 问题在于警报消息已写入视图。
Why does this happen? 为什么会这样? How can I fix this?
我怎样才能解决这个问题?
You need to wrap the injected Razor string in quotes: 您需要将注入的Razor字符串用引号引起来:
@: var msg = "@msg";
Let's say the content of "msg" is "Something" ... then, without the quotes, the rendered script would look like this: 假设“ msg”的内容是“ Something”……那么,不带引号的情况下,呈现的脚本将如下所示:
var msg = Something
Which would be invalid, because there's no variable named "Something". 这将是无效的,因为没有名为“ Something”的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.