更新“父”实体而不是创建它

时间:2013-03-21 20:58:16

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

我列出了一系列项目,我希望顶部工具有一个选项,你点击它并为它添加一个子对象,让我解释一下:

    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,在我的数据库上下文中找到它并创建新对象并指向Father对象我刚刚发现。完成后,返回的视图是:

    @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")
    }

在这个视图中,用户将设置一些变量,如名称和描述,然后提交对象,以便我可以将它持久保存到数据库,问题是我从中得到的对象view将其父亲id作为自己的id,并且Father属性为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;
    }

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

感谢您花时间阅读本文,我们将非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

好吧,试试这个:

将此添加到您的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})<br />

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

另外因为我们需要在这里new{SupportItemId = @Model.FatherId},所以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);
    }