我与联结表有多对多的关系。
public class Contact
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<ContactContactGroup> ContactContactGroups { get; set; }
}
public class ContactContactGroup
{
public int Id { get; set; }
public int ContactId { get; set; }
public int ContactGroupId { get; set; }
public virtual Contact Contact { get; set; }
public virtual ContactGroup ContactGroup { get; set; }
}
public class ContactGroup
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<ContactContactGroup> ContactContactGroups { get; set; }
}
如果我删除一个关系,我会得到以下异常:
Uncaught TypeError: Unable to parse bindings.
Bindings value: text: Contact().Name
Message: Cannot read property 'Name' of null
这是我的删除功能:
that.deleteMe = function (contactContactGroup) {
// Doesn't work also, duplicates entries after second deletion
//contactGroup.ContactContactGroups.remove(contactContactGroup);
contactContactGroup.entityAspect.setDeleted();
};
这是视图:
<div data-bind="foreach: ContactContactGroups">
<div data-bind="text: Contact().Name"></div>
<button data-bind="click: deleteMe">Delete</button>
</div>
我找到了一种使用来自 knockoutjs 的“with”绑定的解决方法:
that.deleteMe = function (contactContactGroup) {
contactGroup.ContactContactGroups.remove(contactContactGroup);
contactContactGroup.entityAspect.setDeleted();
};
<div data-bind="foreach: ContactContactGroups">
<div data-bind="with: Contact">
<div data-bind="text: Name"></div>
</div>
<button data-bind="click: deleteMe">Delete</button>
</div>
但这对我来说似乎有点奇怪。这是一个微风的错误还是我错过了什么?