I've got the following code in one of my views
@if (ViewBag.LoginInfo != null)
{
var loginToken = "@ViewBag.LoginInfo.Token";
var loginUser = "@ViewBag.LoginInfo.UserNameJs";
var notifyUrl = "@ViewBag.LoginInfo.NotificationUrl";
}
The code between { } should be rendered to the page as javascript, however it seems to be getting run as serverside code. I'm aware razor switches back to client code when it sees html in this case the code is valid as C# and javascript. How to I force everthing between { } to be written to the page as javasript?
Thanks
Alternatively use @:
@if (ViewBag.LoginInfo != null)
{
@:var loginToken = @Html.Raw(Json.Encode(ViewBag.LoginInfo.Token);
@:var loginUser = @Html.Raw(Json.Encode(ViewBag.LoginInfo.UserNameJs);
@:var notifyUrl = @Html.Raw(Json.Encode(ViewBag.LoginInfo.NotificationUrl);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With