我有一个对象,其中有 3 个不同的相关/嵌套对象。我可以通过表格编辑它们。我现在想要做的是,当我回到编辑对象时,我希望能够编辑那些子对象——效果很好。除了,当我尝试保存它时 - 子对象只是被复制了。
例如,我为其中一个子对象提供了以下 EditorTemplate:
@model Vineyard.Core.Entities.UsedIngredient
<div class="usedIngredient form-inline">
@if (Model.UsedIngredientId != 0)
{
@Html.HiddenFor(u => u.UsedIngredientId)
}
@if (Model.RecipeId != 0)
{
@Html.HiddenFor(u => u.RecipeId)
}
@Html.LabelFor(r => r.Amount)
@Html.TextBoxFor(r => r.Amount)
@Html.LabelFor(r => r.IngredientName, "Name")
@Html.TextBoxFor(r => r.IngredientName)
@Html.HiddenFor(r => r.Delete, new {@class = "mark-for-delete"})
@Html.LinkToRemoveNestedForm("Remove", "div.usedIngredient", "input.mark-for-delete")
</div>
我将这样的实体保存到数据库中:
if (recipe.RecipeId == 0)
{
context.Recipes.Add(recipe);
}
else
{
Recipe dbObj = context.Recipes.Find(recipe.RecipeId);
dbObj.Name = recipe.Name;
dbObj.Subtitle = recipe.Subtitle;
dbObj.Instructions = recipe.Instructions;
dbObj.Serving = recipe.Serving;
dbObj.PrepTime = recipe.PrepTime;
dbObj.CookingTime = recipe.CookingTime;
dbObj.RecipeImages = recipe.RecipeImages;
dbObj.UsedIngredients = recipe.UsedIngredients;
dbObj.Pairings = recipe.Pairings;
}
context.SaveChanges();
什么会阻止我的子对象被复制?