簡體   English   中英

如何更新一對多關系的父母和所有子女?

[英]How to update parent and all children of One-to-Many relationship?

假設我有以下兩個具有一對多關系的實體,其中一個報表可以具有多個LineItem。 這些實體是使用Entity Foundation 6 Code First創建的。

public class Report
{
    public int ID { get; set;}
    public DateTime ReportDate{ get; set;}
    public virtual ICollection<LineItems> LineItems { get; set;}
}

public class LineItems
{
    public int ID { get; set;}
    public string ItemDescription{ get; set;}
    public virtual Report Report { get; set;}
}

我的控制器和視圖中包含以下內容。

行動:

[HttpPost]
public ActionResult Edit(int? id)
{
    private CodeFirstContext context = new CodeFirstContext();
    ...
    Report rep = context.Reports.Include(x => x.LineItems).Single(x => x.ID == id);
    if(TryUpdateModel(rep, "", new string[] {"ReportDate"}))
    {
        foreach(var item in rep.LineItems)
        {
            if(TryUpdateModel(item, "item", new string[] {"ItemDescription"}))
            {
                context.LineItems.Attach(item);
                context.Entry(item).State = EntityState.Modified;
            }
        }
        context.Entry(rep).State = EntityState.Modified;
        context.SaveChanges();
        ...
    }
}

視圖:

...
@Html.EditorFor(model => model.ReportDate);

@foreach (var item in Model.LineItems)
{
    @html.EditorFor(i => item.ItemDescription);
}
...

我需要能夠在同一視圖中更新單個Report及其所有相關LineItem。 單個報表可以有任意數量的LineItem。 當我使用當前設置提交更新時,所有數據都已成功保存; 但大多數子LineItems的數據不正確。 第一個孩子是正確的,然后所有后續LineItem與第一個LineItem中的數據一起保存,而不是與它們各自輸入中的數據一起保存。

我查看了由頁面生成的html源,所有LineItem輸入具有相同的ID,名稱等,形式為“ item_ItemDescription”或“ item.ItemDescription”。 我很確定發生的事情是因為它們都具有相同的標識符,所以數據只是從具有這些標識符的頁面上的第一個輸入中提取的。

那么如何指定哪個輸入與哪個LineItem一起使用呢? 對我來說,奇怪的是,foreach語句並不會自動為其中的每個輸入創建唯一的標識符。

解決了

正如py3r3str所建議的那樣,我不得不更改視圖以使用for循環而不是foreach並添加HiddenFor來保存每個實體的ID。 我必須更改模型才能使用:

public virtual IList<LineItems> LineItems {get; set;}

因為ICollection沒有與其關聯的索引。

我必須將控制器更改為:

[HttpPost]
public ActionResult Edit(Report report) //changed argument from id to Report
{
    private CodeFirstContext context = new CodeFirstContext();
    ...
    //following line unnecessary with new report argument
    //Report rep = context.Reports.Include(x => x.LineItems).Single(x => x.ID == id);
    if(TryUpdateModel(report, "", new string[] {"ReportDate"}))
    {
        for(int i = 0; i < report.LineItems.Count; i++) //change to for loop
        {
            if(TryUpdateModel(report.LineItems[i], "", new string[] {"ItemDescription"}))
            {
                //removed following line. Update does not work with it in there.
                //context.LineItems.Attach(report.LineItems[i]);
                context.Entry(report.LineItems[i]).State = EntityState.Modified;
            }
        }
        context.Entry(report).State = EntityState.Modified;
        context.SaveChanges();
        ...
    }
}

為了使MVC正確映射您的帖子數據,您必須將視圖代碼更改為:

...
@Html.HiddenFor(model => model.ID);
@Html.EditorFor(model => model.ReportDate);

@for (var i; i < Model.LineItems.Count; i++)
{
    @html.HiddenFor(e => Model.LineItems[i].ID);
    @html.EditorFor(e => Model.LineItems[i].ItemDescription);
}
...

然后,您的模型可以通過MVC控制器進行映射:

...
[HttpPost]
public ActionResult Edit(Report raport)
{
    if (ModelState.IsValid)
    {
        //do save stuff
    }
}
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM