我列出了一系列的项目,我想上面实现,你单击一个选项,并将其添加的子对象为实体,让我解释一下:更新“父”实体,而不是创建它
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