In C# I do this from a controller:
public ActionResult GetCompanies()
{
var vm = new CompanyViewModel { jsonData = JsonConvert.Serialize(_repository.All<Company>()) };
return View(vm);
}
ViewModel looks like this:
public CompanyViewModel
{
public string jsonData {get;set}
}
On the client side:
@model MyWebApp.ViewModels.CompanyViewModel
@Scripts.Render("~/bundles/knockout")
<script type="text/javascript">
var serverData = <!-- Many Things To Do Here -->
</script>
In the part <!-- Many Things To Do Here --> there a few things you can do to put the server data into a JavaScript Object.
What is the best way to do this, because I have had problems with JSON.parse to do with some characters JSON.NET serializes.
Is just putting the plain data in there okay (it works): var serverData = @Html.Raw(@Model.jsonData)
What other things should/can be done here?