Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新“父”实体而不是创建它_C#_.net_Asp.net Mvc_Entity Framework 4 - Fatal编程技术网

C# 更新“父”实体而不是创建它

C# 更新“父”实体而不是创建它,c#,.net,asp.net-mvc,entity-framework-4,C#,.net,Asp.net Mvc,Entity Framework 4,我列出了一系列项目,我想实现一个选项,单击该选项,它将为该实体添加一个子对象,让我解释一下: public class SupportItem { [Display(Name = "Categoría")] [ConcurrencyCheck, Required] public string Type { get; set; } [Key, HiddenInput(DisplayValue = false)] public

我列出了一系列项目,我想实现一个选项,单击该选项,它将为该实体添加一个子对象,让我解释一下:

    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>
如您所见,我收到了一个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")
    }
我做错了什么?为什么我不能创建一个新对象

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

好吧,试试这个:

将其添加到SupportItem类中

然后改变:

@Html.ActionLinkAdd subitem subitem,AddSubitem,new{item.SupportItemId}

@Html.ActionLinkAdd subitem subitem,AddSubitem,Controller Name here new{SupportItemId=@Model.FatherId}

另外,因为我们这里需要FatherId new{SupportItemId=@Model.FatherId},ActionLink需要位于lets say FatherId视图中,例如,在当前只有一个父亲的父亲详细信息中,或者您必须将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);
    }

首先,您将无法编辑不存在的内容。父亲和支持项目之间的关系是什么?是一对一还是一对多问题是我创建的对象在createObject视图中不知何故丢失了。父项和支持项之间的关系是:每个supportItem也不能有一个类型为support item的父项,其定义在此属性上:[HiddeniInputDisplayValue=false]公共虚拟支持项父项{get;set;}父项和支持项之间的关系是什么?是一对一还是一对多?是一对一,每个支持项目只能有一个父亲
        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;
    }
public class SupportItem
{
  [Key]
  [HiddenInput(DisplayValue = false)]
  [ForeignKey("Father"), DatabaseGenerated(DatabaseGeneratedOption.None)]       
  public int SupportItemId { get; set; }

  public virtual Father Father { get; set; }

  ...................
  ...................
}
    [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);
    }