0

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

    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; }
}

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

    @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 和父亲属性为空,因此我最终更新了我想用这个方法添加一个孩子的父亲对象:

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;
    }

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

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

4

1 回答 1

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 />

同样因为我们需要FatherId new{SupportItemId = @Model.FatherId},所以ActionLink需要在Father 视图中,例如在Father Details 中只有一个Father 是当前的,或者您必须将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);
    }
于 2013-03-21T22:13:19.223 回答