Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force razor to switch back to client side code?

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

like image 514
Gavin Avatar asked Nov 30 '22 01:11

Gavin


1 Answers

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);     
}
like image 140
Stark Avatar answered Dec 06 '22 04:12

Stark