The Json
method that you call in your controller is designed to return a JsonResult
, it does not create a JSON string. You should use this method to return json from an ajax call.
To return a JSON string to the view, use something like this.
JavaScriptSerializer serializer = new JavaScriptSerializer(); ViewBag.InitialData = serializer.Serialize(people);
Then in your view code
<script> var initialData = '@Html.Raw(ViewBag.InitialData)'; .... </script>
To answer the second question. To pass global list data, for example, simply define a new ContactsList
eg class
public class ContactsList { public string Name { get;set; } public string Owner { get;set; } public IList<People> People { get;set; } }
Fill it and pass it instead of the JavascriptSerializer
. You will obviously need to configure js ContactsModel
accordingly.
EDIT
Here's a jsfiddle showing the necessary changes.
http://jsfiddle.net/madcapnmckay/jRjwU/
Hope this helps.
source share