Note that there are some explanatory texts on larger screens.

plurals
  1. POChild JSON object undefined when its not
    primarykey
    data
    text
    <p>I have an array of JSON objects in the page source that all work except a child object (category).</p> <p>Here is the code in cshtml:</p> <pre><code> &lt;script type="text/javascript"&gt; var initialData = @Html.Raw(Json.Encode(ViewBag.OfferItems)); &lt;/script&gt; </code></pre> <p>Here is the resulting page source:</p> <pre><code> &lt;script type="text/javascript"&gt; var initialData = [{"Id":1,"Name":"Item1","ProductVariantLinks":[{"category":{"Id":2,"Name":"Basic Pizza","Products":null},"product":{"Id":1,"Name":"Margherita","Description":null,"ProductVariants":null},"productVariant":{"Id":1,"Name":"10 inch"}},{"category":{"Id":2,"Name":"Basic Pizza","Products":null},"product":{"Id":2,"Name":"Zeno","Description":null,"ProductVariants":null},"productVariant":{"Id":4,"Name":"8 inch"}}]},{"Id":2,"Name":"Item2","ProductVariantLinks":[]}]; &lt;/script&gt; </code></pre> <p>As far as I can tell category is there and contains properties, but it appears as undefined in IE's debugger.</p> <p><img src="https://i.stack.imgur.com/bmiRU.png" alt="IE Debugger showing category undefined"></p> <p>Is there something I'm missing?</p> <p>P.S. the JSON is valid.</p> <p><strong>Update</strong></p> <p>I'm using knockoutjs and category is in inialdata until it does ko.applybindings. I'm not sure why it would do this, code below:</p> <pre><code>/// &lt;reference path="jquery-1.5.1.js" /&gt; /// &lt;reference path="knockout-2.0.0.js" /&gt; var ProductVariantLink = function() { var self = this; self.category = ko.observable(); self.product = ko.observable(); self.productVariant = ko.observable(); // Whenever the category changes, reset the product selection self.category.subscribe(function() { self.product(undefined); self.productVariant(undefined); }); }; var OfferItem = function() { var self = this; self.Name = new String(); self.ProductVariants = new Array(); }; var SpecialOfferItemModel = function (specialOfferItems) { var self = this; self.specialOfferItems = ko.observableArray(ko.utils.arrayMap(specialOfferItems, function (specialOfferItem) { return { Id: specialOfferItem.Id, Name: specialOfferItem.Name, ProductVariants: ko.observableArray(specialOfferItem.ProductVariantLinks) }; })); self.addSpecialOfferItem = function () { self.specialOfferItems.push({ Id: "", Name: "", ProductVariants: ko.observableArray() }); }; self.removeSpecialOfferItem = function (specialOfferItem) { self.specialOfferItems.remove(specialOfferItem); }; self.addProductVariant = function (specialOfferItem) { specialOfferItem.ProductVariants.push(new ProductVariantLink()); }; self.removeProductVariant = function (ProductVariant) { $.each(self.specialOfferItems(), function () { this.ProductVariants.remove(ProductVariant) }) }; self.save = function () { var OfferItems = new Array(); $.each(self.specialOfferItems(), function () { var item = this; var offer = new OfferItem(); offer.Name = item.Name; $.each(item.ProductVariants(), function () { offer.ProductVariants.push(this.ProductVariant); }); OfferItems.push(offer); }); self.lastSavedJson(JSON.stringify(ko.toJS(self.specialOfferItems()), null, 2)); return false; }; self.lastSavedJson = ko.observable(""); }; var model = new SpecialOfferItemModel(initialData); ko.applyBindings(model); $(function () { $('#myForm').submit(function () { model.save(); }); }); &lt;table class="specialOfferItemsEditor"&gt; &lt;tr&gt; &lt;th&gt; &lt;/th&gt; &lt;th&gt; Name &lt;/th&gt; &lt;th&gt; ProductVariants &lt;/th&gt; &lt;/tr&gt; &lt;tbody data-bind="foreach: specialOfferItems"&gt; &lt;tr&gt; &lt;td&gt; &lt;div&gt; &lt;a href="#" data-bind="click: $root.removeSpecialOfferItem"&gt;Delete&lt;/a&gt;&lt;/div&gt; &lt;/td&gt; &lt;td&gt; &lt;input data-bind="value: Name" /&gt; &lt;/td&gt; &lt;td&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt; Category &lt;/th&gt; &lt;th&gt; Product &lt;/th&gt; &lt;th&gt; ProductVariant &lt;/th&gt; &lt;/tr&gt; &lt;tbody data-bind="foreach: ProductVariants"&gt; &lt;tr&gt; &lt;td&gt; &lt;select data-bind='options: ProductCategories, optionsText: "Name", optionsCaption: "Select...", value: category, uniqueName: true'&gt; &lt;/select&gt; &lt;/td&gt; &lt;td data-bind="with: category"&gt; &lt;select data-bind='options: Products, optionsText: "Name", optionsCaption: "Select...", value: $parent.product, uniqueName: true' &gt; &lt;/select&gt; &lt;/td&gt; &lt;td data-bind="with: product"&gt; &lt;select data-bind='options: ProductVariants, optionsText: "Name", optionsCaption: "Select...", value: $parent.ProductVariant, uniqueName: true' &gt; &lt;/select&gt; &lt;/td&gt; &lt;td&gt; &lt;a href='#' data-bind='click: $root.removeProductVariant'&gt;Delete&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;a href='#' data-bind='click: $root.addProductVariant'&gt;Add Product Variant&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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