Click here to Skip to main content
15,860,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
I did a multilevel bind using index:

<input type="hidden" name="parent.Index" value="aaa" />
<input type="text" name="parent[aaa].Category" value="Cat1" />
<input type="text" name="parent[aaa].SubCategory" value="SubCat1" />

<input type="hidden" name="parent.Index" value="bbb" />
<input type="text" name="parent[bbb].Category" value="Cat2 />
<input type="text" name="parent[bbb].SubCategory" value="SubCat2" />

<input type="hidden" name="parent.Index" value="ccc" />
<input type="text" name="parent[ccc].Category" value="Cat3" />
<input type="text" name="parent[ccc].SubCategory" value="SubCat3" />

<input type="hidden" name="parent[ccc].products.Index" value="123" />
<input type="text" name="parent[ccc].products[123].Name" value="Chips" />
<input type="text" name="parent[ccc].products[123].Price" value="2.23" />

The parent.index models are bond correctly but the parent[aaa].products.Index are not.

If I have 3 categories, aaa, bbb and ccc but for exemple only ccc has a product, it binds the product to aaa instead of ccc, in other words, any gap of products between categories it bind the product to the previous parent with no product

Thanks for any help.

HTML

Posted

I'd suggest to read this: Hierarchical Data Binding in ASP.NET[^]
 
Share this answer
 
Comments
Israel Thieme 18-Dec-14 10:33am    
Thanks for the reply but it's not exactly the problem I'm having.
XML
I'm having trouble to bind a 2 level nested model. Here is what I've come up so far.

Model:

RequestSignature Model:

public class RequestSignature
{
    public int Id { get; set; }
    public string name { get; set;}
    public virtual ICollection<Pages> Pages { get; set; }
    public virtual ICollection<SignaturesImages> SignaturesImages { get; set; }
}
Pages Model:

public class Pages
{
    public int Id { get; set; }
    public string PageName {get; set;}
    public int RequestSignatureId { get; set; }
    [ForeignKey("RequestSignatureId")]
    public virtual RequestSignature RequestSignature { get; set; }
    public virtual ICollection<Signatures> Signatures { get; set; }
}
And Signatures Model:

public class Signatures
{
    public int Id { get; set; }<br>
    public Date SignatureDate { get; set;}
    public int? PagesId { get; set; }
    [ForeignKey("PagesId")]
    public virtual Pages Pages { get; set; }
}
Item 1:

<input type="text" name="RequestSignature.name" value="Joe Smith" />

<input type="hidden" name="RequestSignature.Pages.Index" value="aa1" /><br>
<input type="text" name="RequestSignature.Pages[aa1].PageName" value="My Page /><br>
<input type="hidden" name="RequestSignature.Pages[aa1].Signatures.Index" value="aa2" /><br>
<input type="text" name="RequestSignature.Pages[aa1].Signatures[aa2].SignatureDate" value="12/22/2014" />
Item 2:

<input type="text" name="RequestSignature.name" value="MattJohnson" />

<input type="hidden" name="RequestSignature.Pages.Index" value="bb1" /><br>
<input type="text" name="RequestSignature.Pages[bb1].PageName" value="My Page />
Item 3:

<input type="text" name="RequestSignature.name" value="Bob Dylan" />

<input type="hidden" name="RequestSignature.Pages.Index" value="cc1" /><br>
<input type="text" name="RequestSignature.Pages[cc1].PageName" value="My Page /><br>
<input type="hidden" name="RequestSignature.Pages[cc1].Signatures.Index" value="cc2" /><br>
<input type="text" name="RequestSignature.Pages[cc1].Signatures[cc2].SignatureDate" value="12/22/2014" />

The problem is that since the Item 2 has no Signature(only one level) and it does not have a child hidden field, the Signature on Item 3 is being bonded to Item 2.

To save, I'm calling:

db.RequestSignature.Add(RequestSignature);
db.SaveChanges();

If I stop to debug before saving, It shows the Signature being bonded to the correct Item, even if there is a gap of signatures between Items but when the save happens, like I said, If the Item 2 has no signature, it saves the Signature3 as a child of Signature2

Thanks for any help....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900