【问题标题】:Updating 'Parent' entity instead of creating it更新“父”实体而不是创建它
【发布时间】:2013-03-21 20:58:15
【问题描述】:

我列出了一系列项目,我希望在顶部实现一个选项,您可以单击它并为该实体添加一个子对象,让我解释一下:

    public class SupportItem
   {
    [Display(Name = "Categoría")]
    [ConcurrencyCheck, Required]

    public string Type { get; set; }

    [Key, HiddenInput(DisplayValue = false)]       
    public int SupportItemId { get; set; }

    [Display(Name = "Nombre")]
    [ConcurrencyCheck,Required]

    public string Name { get; set; }

    [ConcurrencyCheck]
    [Display(Name = "Descripción Corta")]
    [DataType(DataType.MultilineText)]
    [Required]
    public string Description { get; set; }

     [HiddenInput(DisplayValue = false)]
    public virtual SupportItem Father { get; set; }

    [Display(Name = "Descripción detallada")]
    [DataType(DataType.MultilineText)]
    [Required]
    public string LongDescription { get; set; }
    [HiddenInput(DisplayValue = false)]
    public bool Children { get; set; }
}

现在你可以看到,这个实体有一个父项,它的类型是 SupporItem。现在我要做的是将它们全部列出并添加一个选项,让您可以轻松地为您选择的项目添加一个子项,这是视图定义:

    @model IEnumerable<Domain.Entities.SupportItem>

    @{
        ViewBag.Title = "IndexSupportItems";
        Layout = "~/Views/Shared/_AdminLayout.cshtml";
    }

    <h2>Index Support Items</h2>

    <p>
    @Html.ActionLink("Crear nuevo item principal", "Create")
    </p>
    <table class="Grid">
    <tr>
    <th>
        Tipo
    </th>
    <th>
        Nombre
    </th>
    <th>
        Descripción
    </th>
    <th> 
        Acciones
    </th>                
    </tr>

     @foreach (var item in Model) 
    {
    <tr>
    <td>@item.Type</td>
    @if(item.Children)
    {
    <td>@Html.ActionLink(item.Name,"ListChildren", new{item.SupportItemId})</td>
    }
    else
    {<td>@item.Name</td>

    }
    <td>@item.Description</td>
        <td>
       @Html.ActionLink("Delete","DeleteSupportItem", new{item.Father.SupportItemId})<br />
        @Html.ActionLink("Add subitem sub-item","AddSubitem", new{item.SupportItemId})<br />
        @Html.ActionLink("Edit","EditSupportItem", new{item.SupportItemId})    
    </td>

</tr>
    }

    </table>

现在您可以看到,执行此操作的操作链接指向一个名为 AddSubitem 的方法,其实现如下:

    public ViewResult AddSubitem(int supportItemId)
    {
        SupportItem child = new SupportItem() { Father = repo.GetSupportItemFromId(supportItemId) };

        return View(child);
    }

如您所见,我收到一个 supportItemId,它是来自父实体(我要添加新子实体的实体)的 id,在我的数据库上下文中找到它并创建新对象并指向父对象我刚发现。这样做之后,它返回的视图是这样的:

    @model Domain.Entities.SupportItem

    @{
        ViewBag.Title = "AddSubitem";
    }

    <h2>AddSubitem</h2>

    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)

        <fieldset>
            <legend>Support Item</legend>

            <div class="editor-label">
        @Html.LabelFor(model => model.Type)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Type)
        @Html.ValidationMessageFor(model => model.Type)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Description)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Description)
        @Html.ValidationMessageFor(model => model.Description)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.LongDescription)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.LongDescription)
        @Html.ValidationMessageFor(model => model.LongDescription)
    </div>



            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }

    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>

    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }

在这个视图中,用户将设置一些变量,例如名称和描述,然后提交对象,这样我就可以将它持久化到数据库中,问题是我从中得到的对象视图有它的父亲 id 作为它自己的 id 并且父亲属性是 null 因此我最终更新了我想用这个方法添加一个孩子的父亲对象:

public bool SaveSupportItem(SupportItem supportItem) { bool retorno = false;

        if (supportItem.SupportItemId == 0)
        {
            context.SupportItems.Add(supportItem);
            supportItem.Father.Children = true;
            retorno = true;
        }
        else
        {
            SupportItem itemDB = context.SupportItems.Find(supportItem.SupportItemId);
            if (itemDB != null)
            {
                itemDB.Name = supportItem.Name;
                itemDB.Type = supportItem.Type;
                itemDB.LongDescription = supportItem.LongDescription;
                itemDB.Description = supportItem.Description;
                retorno = true;
            }
        }
        context.SaveChanges();
        return retorno;
    }

我在这里做错了什么?为什么我不能创建一个新对象?

感谢您抽出宝贵时间阅读本文,我们将不胜感激!

【问题讨论】:

  • 首先,您将无法编辑不存在的内容。还有FatherSupportItem之间的关系是什么?是one-to-one还是one-to-many
  • 问题是我创建的对象在 createObject 视图中以某种方式丢失。父项和支持项之间的关系是:每个支持项不能有一个父项也是支持项类型的,它定义在这个属性上:[HiddenInput(DisplayValue = false)] public virtual SupportItem Father { get;放; }
  • FatherSupportItem是什么关系?是one-to-one 还是one-to-many
  • 是一对一的,每个支持项只能有一个父亲

标签: c# .net asp.net-mvc entity-framework-4


【解决方案1】:

好吧,试试这个:

将此添加到您的 SupportItem 类

public class SupportItem
{
  [Key]
  [HiddenInput(DisplayValue = false)]
  [ForeignKey("Father"), DatabaseGenerated(DatabaseGeneratedOption.None)]       
  public int SupportItemId { get; set; }

  public virtual Father Father { get; set; }

  ...................
  ...................
}

然后改变:

@Html.ActionLink("Add subitem sub-item","AddSubitem", new{item.SupportItemId})&lt;br /&gt;

@Html.ActionLink("Add subitem sub-item","AddSubitem", "Controller Name here" new{SupportItemId = @Model.FatherId})&lt;br /&gt;

另外,因为我们在此处需要 new{SupportItemId = @Model.FatherId} 的父亲 ID,所以 ActionLink 需要在父亲视图中,例如在父亲详细信息中,其中只有单个父亲是当前的,或者您必须将 supportItem 与特定父亲相关联。

假设您使用的是 ViewModel,您的控制器可能如下所示:

    [HttpGet]
    public ActionResult CreateSuppo(int supportItemId)
    {
        var model = new CreateSupportItemViewModel ();
        model.SupportItemId= supportItemId;
        return View(model);
    }

    [HttpPost]
    public ActionResult Create(CreateSupportItemViewModel viewModel)
    {
        if(ModelState.IsValid)
        {
            var father= db.Fathers.Single(f => f.FatherId == viewModel.SupportItemId);
            var supportItem= new SupportItem();
            supportItem.Name = viewModel.Name;
            ....................
            .................
            father.SupportItems.Add(supportItem);

            db.SaveChanges();               
        }
        return View(viewModel);
    }

【讨论】:

    猜你喜欢
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多