Reputation: 9081
i have a javascript
alert in C# code like this
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?
Upvotes: 1
Views: 2980
Reputation: 102743
You need to wrap the injected Razor string in quotes:
@: var msg = "@msg";
Let's say the content of "msg" is "Something" ... then, without the quotes, the rendered script would look like this:
var msg = Something
Which would be invalid, because there's no variable named "Something".
Upvotes: 3