0

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.

1 Answer 1

1

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"]));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I have used the technique that all are suggested to me. I just wanted to know if I can directly pass the session object to angularjs function as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.