私は一連のアイテムをリストしていますが、クリックしてそのエンティティの子オブジェクトを追加するオプションをトップに実装したいと思います。説明させてください。
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タイプのFatherがあります。今、私がやりたいのは、それらすべてをリストし、選択したアイテムの子を簡単に追加できるオプションを追加することです。ビューの定義は次のとおりです。
@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);
}
ご覧のとおり、親エンティティ(新しい子を追加するエンティティ)からidであるsupportItemIdを受け取り、データベースコンテキストでそれを見つけて、新しいオブジェクトを作成し、見つけたばかりの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")
}
このビューでは、ユーザーは名前や説明などのいくつかの変数を設定し、オブジェクトを送信してデータベースに永続化できるようにします。問題は、このビューから取得したオブジェクトの父親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;
}
私はここで何が間違っているのですか?新しいオブジェクトを作成できないのはなぜですか?
時間を割いてこれを読んでくれてありがとう、どんな助けでも本当にありがたいです!