简体   繁体   English

ASP.NET MVC 5 如何在同一视图中编辑父级和列表<>类型子级

[英]ASP.NET MVC 5 How to edit Parent and List<> type Child in same view

I need to edit a class and its child of List type in the same view but I don't know how to make the child editing in the same view.我需要在同一视图中编辑 class 及其 List 类型的子项,但我不知道如何在同一视图中进行子项编辑。

Let's say I have these classes:假设我有这些课程:

public class Parent
{
    public int PValue1 { get; set; }
    public int PValue2 { get; set; }
    public int PValue3 { get; set; }

    public virtual List<ChildItem> Childs { get; set; }
}

public class ChildItem
{
    public int CValue1 { get; set; }
    public int CValue2 { get; set; }
}

The view:风景:

@model MyNamespace.Parent

@using (Html.BeginForm("SaveParent", "Index"))
{
    <label>Parent Value 1</label>
    @Html.TextBoxFor(m=> m.PValue1, "", new { @class = "form-control" })

    <label>Parent Value 2</label>
    @Html.TextBoxFor(m=> m.PValue2, "", new { @class = "form-control" })

    <label>Parent Value 3</label>
    @Html.TextBoxFor(m=> m.PValue3, "", new { @class = "form-control" })

    @*  WHAT TODO? *@

    <table>
        <thead>
            <tr>
            <th>Child Value 1</th>
            <th>Child Value 2</th>
            </tr>
        </thead>
        <tbody>
        @foreach(ChildItem item in Model.Childs){
            <tr>
            <td>@item.CValue1</td>
            <td>@item.CValue2</td>
            </tr>
        }
        </tbody>
    </table>

    <button type="submit" class="btn btn-primary">Save</button>
}

I need to make an insert code in the "WHAT TODO" area for the Childs child.我需要在Childs孩子的“WHAT TODO”区域中插入代码。 I tried using PartialView with something like:我尝试将 PartialView 与以下内容一起使用:

@Html.Partial("_MyPartialView", Model.Childs)

And then in the PartialView create another form to use a Insert(ChildItem child) controller but I also couldn't achieve this.然后在 PartialView 创建另一个表单以使用Insert(ChildItem child) controller 但我也无法实现这一点。 I didn't know how to make the second form add to the model because it was a List.我不知道如何将第二种形式添加到 model 因为它是一个列表。

It's probably obvious right now, but I'm new to ASP.NET MVC and used to old WebForms, so still getting to understand somethings.现在可能很明显,但我是 ASP.NET MVC 的新手,并且习惯了旧的 WebForms,所以仍然了解一些东西。

My goal is to accomplish a working view like this:我的目标是完成这样的工作视图:

在此处输入图像描述

Ok, so with further study, I found out I dont need nested forms ou partial views, but multiple submit buttons with multiple form actions.好的,所以通过进一步研究,我发现我不需要嵌套的 forms ou 部分视图,而是具有多个表单操作的多个提交按钮。 I got what I needed and apparently it was really basic, so I thought I could post my own solution to my own question and maybe help anybody else with same problem.我得到了我需要的东西,显然它真的很基础,所以我想我可以发布我自己的解决方案来解决我自己的问题,也许可以帮助其他有同样问题的人。 So, starting with the view I replaced this:所以,从我替换的视图开始:

    @*  WHAT TODO? *@

With this:有了这个:

    <label>Child Value 1</label>
    <input type="number" name="CValue1" />
    <label>Child Value 2</label>
    <input type="number" name="CValue1" />
    <!--This is the real trick! -->
    <input type="submit" formaction="AddChild" formmethod="post" />  

The formaction="AddChild" fires the AddChild() action in the controller, submiting the Parent model and the CValue1 and CValue2 inputs. formaction="AddChild"触发 controller 中的 AddChild() 操作,提交父 model 以及CValue1CValue2输入。 But the Childs property of the model was always null ,so, after executing the AddChild action, I was getting only the last added ChildItem .但是 model 的Childs属性始终是null ,因此,在执行AddChild操作后,我只得到最后添加的ChildItem To solve this, I had to make my controller recognize the List<ChildItem> in my Parent sending it separatelly in the post.为了解决这个问题,我必须让我的 controller 识别我Parent中的List<ChildItem>在帖子中单独发送。 So, I had to change the list view from this:所以,我不得不从这个改变列表视图:

@foreach(ChildItem item in Model.Childs){
   <tr>
     <td>@item.CValue1</td>
     <td>@item.CValue2</td>
   </tr>
}

To this:对此:

 @for(int x = 0; x < Model.Childs.Count; x++){ 
  <tr>
     <td>@Html.DisplayFor(m => m.Childs[x].CValue1)</td>
     <td>@Html.DisplayFor(m => m.Childs[x].CValue2)</td>
     <td> <button type="submit" formaction="DeleteChild" name="ChildID" value="@Model.Childs[x].ID" formmethod="post">X</button>
      @Html.HiddenFor(m => m.Childs[x].ID)
      @Html.HiddenFor(m => m.Childs[x].CValue1)
      @Html.HiddenFor(m => m.Childs[x].CValue2)
     </td>
  </tr>               
  }

The hidden fields store all the info for the controller binding.隐藏字段存储 controller 绑定的所有信息。 Because of the index x for each line, when submiting the form, the controller can bind all child elements to the Child property correctly.由于每行的索引x ,在提交表单时,controller 可以正确地将所有子元素绑定到 Child 属性。

The controller for adding the Child:用于添加子的 controller:

public ActionResult AddChild(Parent parent, int CValue1, int CValue2){
  if (parent.Childs == null)
    parent.Childs = new List<ChildItem>();
  parent.Childs.Add(new ChildItem(){
    ID = parent.Childs.Count,  //This will be removed/improved later
    CValue1 = CValue1,
    CValue2 = CValue2
  });
  return View("Index", parent);
}

I also implemented the DeleteChild action:我还实现了DeleteChild操作:

public ActionResult DeleteChild(Parent parent, int ChildID){
   if (parent != null)
      parent.Childs.Remove(parent.Childs.FirstOrDefault(c => c.ID == ChildID));
      return View("Index", parent);
}

It still missing all the database code, but thats the next step!它仍然缺少所有数据库代码,但这是下一步!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM