我列出了一系列的項目,我想上面實現,你單擊一個選項,並將其添加的子對象爲實體,讓我解釋一下:更新「父」實體,而不是創建它
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是從父entitity的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和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;
}
我在做什麼錯在這裏?爲什麼我不能創建一個新的對象?
感謝您花時間閱讀本文,我們將非常感謝您的幫助!
首先,你將無法編輯的東西不存在。另外'Father'和'SupportItem'之間的關係是什麼?它是'一對一'還是'一對多' – Komengem 2013-03-21 21:37:21
問題是我創建的對象在createObject視圖上以某種方式丟失了。父和支持項之間的關係是:每個支持項都可以不能有一個類型爲支持項的父親,它在此屬性上定義:[HiddenInput(DisplayValue = false)] public virtual SupportItem Father {get;組; } – oskar132 2013-03-21 21:42:23
'Father'和'SupportItem'之間的關係是什麼?它是「一對一」還是「一對多」? – Komengem 2013-03-21 21:43:32