I am using the latest SPA template from https://github.com/aspnet/JavaScriptServices
How can I pass a serialized object from MVC to Angular? Is this possible?
I am using the latest SPA template from https://github.com/aspnet/JavaScriptServices
How can I pass a serialized object from MVC to Angular? Is this possible?
At first glance you have two options. If you use server-side rendering, you can use the
asp-prerender-data
tag helper, otherwise, you can adopt the classical approach with data attributes.For example (illustrating both approaches at the same time):
Index.cshtml
boot-server.ts
Now in boot-server.ts, if you use server-side prerendering, you can access the passed server data via
params.data
. Also here you should pass your server data to the client side (boot-client.ts
) along with the generated html. For example:boot-client.ts
If you use server-side rendering, the passed data (
serverData: params.data
) will be attached to thewindow
object. For example, you could have something like:If you have passed the object via data attributes, then:
JavaScriptHelper.cs
Just a helper class serializing an object to string with some settings. There might be a smarter way, but.....
p.s. Before testing don't forget to run:
webpack --config webpack.config.js