2013-03-21 24 views
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; } 
} 

现在你可以看到,这个实体有一个类型为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; 
    } 

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

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

+0

首先,你将无法编辑的东西不存在。另外'Father'和'SupportItem'之间的关系是什么?它是'一对一'还是'一对多' – Komengem 2013-03-21 21:37:21

+0

问题是我创建的对象在createObject视图上以某种方式丢失了。父和支持项之间的关系是:每个支持项都可以不能有一个类型为支持项的父亲,它在此属性上定义:[HiddenInput(DisplayValue = false)] public virtual SupportItem Father {get;组; } – oskar132 2013-03-21 21:42:23

+0

'Father'和'SupportItem'之间的关系是什么?它是“一对一”还是“一对多”? – Komengem 2013-03-21 21:43:32

回答

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需要处于可以说在父亲详细父亲视图e.g只有单亲父亲是当前或东西,你必须将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); 
    } 
相关问题