Note that there are some explanatory texts on larger screens.

plurals
  1. POmvc - binding JSON object
    primarykey
    data
    text
    <p>I am following a simple video tutorial for Knockout.js by Steve Sanderson: <a href="http://channel9.msdn.com/Events/MIX/MIX11/FRM08" rel="nofollow">http://channel9.msdn.com/Events/MIX/MIX11/FRM08</a></p> <p>At the very end of it he performs an AJAX call to show how can you use knockout to process data on the server. I repeat all of what he is doing, but for some reason my JSON object doest bind to a POCO class correctly. This is an object I send from the view:</p> <pre><code>{"firstName":"Bartosz","lastName":"Malinowski","friends":[{"name":"Zofia"},{"name":"Zenon"}],"fullName":"Bartosz Malinowski"} </code></pre> <p>and then use this code to read it in the controller:</p> <pre><code>public JsonResult Save(Person person) { var message = string.Format("Saved {0} {1}", person.firstName, person.lastName); return Json(new { message }); } public class Person { public string firstName { get; set; } public string lastName { get; set; } public string fullName { get; set; } public ICollection&lt;Friend&gt; friends { get; set; } } public class Friend { public string Name { get; set; } } } </code></pre> <p>My code on the client side looks like this:</p> <pre><code>'@{ ViewBag.Title = "Home Page"; } &lt;script src="../../Scripts/jquery.tmpl.js" type="text/javascript"&gt;&lt;/script&gt; &lt;p&gt;First name: &lt;input data-bind="value: firstName"/&gt;&lt;/p&gt; &lt;p&gt;Last name: &lt;input data-bind="value: lastName"/&gt;&lt;/p&gt; &lt;p&gt;Full name: &lt;span data-bind="text: fullName"&gt;&lt;/span&gt;&lt;/p&gt; &lt;h2&gt;Friends (&lt;span data-bind="text: friends().length"&gt;&lt;/span&gt;)&lt;/h2&gt; &lt;ul data-bind="template: { name: 'friendsTemplate', foreach: friends }"&gt;&lt;/ul&gt; &lt;script id="friendsTemplate" type="text/html"&gt; &lt;li&gt; &lt;input data-bind="value: name"/&gt; &lt;button data-bind="click: remove"&gt;Remove&lt;/button&gt;&lt;/li&gt; &lt;/script&gt; &lt;button data-bind="click: addFriend, enable: friends().length &lt; 5"&gt;Add Friend&lt;/button&gt; &lt;button data-bind="click: save"&gt;Save&lt;/button&gt; &lt;script type="text/javascript"&gt; function friend(name) { return { name: ko.observable(name), remove: function () { viewModel.friends.remove(this); } }; } var viewModel = { firstName: ko.observable("Bartosz"), lastName: ko.observable("Malinowski"), friends: ko.observableArray([new friend("Zofia"), new friend("Zenon")]), addFriend: function () { this.friends.push(new friend("Jan")); }, save: function () { $.ajax({ url: "@Url.Action("Save")", type: "post", data: ko.toJSON(this), contenttype: "application/json", success: function (result) { alert(result.message) } }); } }; viewModel.fullName = ko.dependentObservable(function () { return this.firstName() + " " + this.lastName(); }, viewModel); ko.applyBindings(viewModel); &lt;/script&gt; </code></pre> <p>When I run it in debug mode and check person in the Save method parameter I've got null value for each Person property. So it just doesn't bind to me??</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload