1

I'm trying to get an SPA working with the following basic models:

public class Owner
{
    public int OwnerId { get; set; }
    [Required]
    public string Name { get; set; }

    public virtual ICollection<TodoItem> TodoItems { get; set; }
}

public class TodoItem
{
    public int TodoItemId { get; set; }
    [Required]
    public string Title { get; set; }
    public bool IsDone { get; set; }

    public int OwnerId { get; private set; }
    public virtual Owner Owner { get; set; }
    public virtual ICollection<ActionItem> ActionItems { get; set; }
}

public class ActionItem
{
    public int Id { get; set; }
    public string Name { get; set; }

    public int TodoItemId { get; private set; }
    public virtual TodoItem TodoItem { get; private set; }
}

and in my view I tried this:

<div data-bind="foreach: ActionItem">
<div data-bind="attr:{id:Id}">
    <span data-bind="text: Name"></span><br /> <!-- works -->
    <span data-bind="text: TodoItem().Name"></span><br /> <!-- doesn't work -->
    <span data-bind="text: TodoItem().Owner().Name"></span> <br /> <!-- doesn't work -->
</div>
</div>

I also tried adding a computed property to ActionItem containing the owner name:

public string OwnerName
{
get { return this.TodoItem.Owner.Name; }
}

and calling:

<span data-bind="text: OwnerName"></span><br /> <!-- doesn't work -->

but if I add a different static string and set the value manually:

public string SomethingElse { get; set; }

then this works fine:

<span data-bind="text: SomethingElse"></span><br /> <!-- works -->

What am I doing wrong?

Cheers

nugget
  • 155
  • 1
  • 1
  • 16
  • How looks your controller? Are you doing an Include "Owner" in your TodoItem query? Also that might lead to cyclic references... The paret-chidl-parent references are not really covered as it seems right now. It would lead to an infinite loop in serialisation. I created a DTO for that reason, wehre the child is referencing the parent only through his string name, id and not the complete reference to the parent object. – Obiwan007 Mar 24 '12 at 07:23
  • hey - sorry about the late reply. I had to step away from the machine for a little while :) The cyclic references error is exactly what I got and after much research it seems the alternate fix of swapping out the serialiser with json.net causes other issues. So it looks like I'll have to stick with mvc3 for now and update later. Using a string key is a good idea - I'll try that tonight. – nugget Mar 27 '12 at 06:07

2 Answers2

1

As suggested by Obiwan007, you case solve the serialization issues by setting the back reference to NULL. You can also apply the [IgnoreDataMember] attribute if you are using Code First.

Upshot only makes the parent entities observable and not their child entities. So when using the built-in mapping functionality, data-binding to child entities will not work.

The solution is to manually create your client-side entities in Javascript and map them yourself. I posted a working solution in response to a different question: https://stackoverflow.com/a/10010695/1226140

Community
  • 1
  • 1
Bart Jolling
  • 595
  • 6
  • 21
0

Try to use the $parent variable:

    <span data-bind="text: $parent.Name"></span> <br />
    <span data-bind="text: TodoItem().$parent.Name"></span> <br />
FHeNuS
  • 101
  • 5