Cham
Cham

Reputation: 857

Passing session object to angularjs function in razor view MVC

I need to pass the asp.net session object to a function that I call in ng-init. something like this;

ng-init=initUser( @((User)Session["userSession"]) )

I just want to pass my "User" object to the initUser method declared in angularjs. Is there a way to do it ?.

I have searched for a solution and all are suggested to call an action method in the controller inside the "initUser" method and fetch the session object from the server and assign it to angularjs scope variable.

Thanks.

Upvotes: 0

Views: 875

Answers (1)

Lasitha Petthawadu
Lasitha Petthawadu

Reputation: 763

The solution you searched and got as the response is more elegant since the front-end would query the data from the server and serve it through an API.

However if you want to inject the .NET session to javascript you could Json encode it to a variable and add it to the scope.

var json = @Html.Raw(Json.Encode(@((User)Session["userSession"]));

Upvotes: 1

Related Questions