gpt4 book ai didi

asp.net-mvc - MVC - 一次性创建对象和相关对象

转载 作者:行者123 更新时间:2023-12-04 14:12:46 24 4
gpt4 key购买 nike

我想在同一个 View 中创建一个带有子/相关对象的父对象。
一个例子是:创建一个父亲(有一些名字)和他的所有儿子(有他们的名字)。我创建了一个 View 模型:

public class FatherViewModel {
public Father father {get; set;} // has 1 property Name
public List<Son> {get; set;} // has 1 property Name
}

我的问题是,当执行帖子时,如何从 View 中获取 Sons 列表?
我尝试对每个 Son id 使用 HiddenFor,但无论如何,返回 Controller 时列表为空。

更新:

我尝试了下面描述的 Shyju 的编辑器模板示例,但从未调用过我的编辑器。
我有 1 个对象:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int? FatherId { get; set; }
public virtual ICollection<Person> Children { get; set; }
}

我这样做了:
  • 为 Person 搭建了一个完整的 Controller ,具有索引、创建、编辑...
  • 在 Views->Person
  • 中创建 EditorTemplates 文件夹
  • 创建 Person.cshtml:
    @model TestEditorTemplate.Models.Person
    <div>
    <h4>Child</h4>
    @Html.TextBoxFor(s => s.Name)
    @Html.HiddenFor(s => s.Id)
    </div>
  • 已添加 @Html.EditorFor(m => m.Children)创建.cshtml

  • 问题:
  • 怎么可能@Html.EditorFor(m => m.Children)可能与
    编辑器模板当m.ChildrenPerson 的集合而不是一个Person ?
  • 我想同时创建(而不是编辑)一个包括 child 在内的父亲。这意味着我没有要传递给 Create View 的 ID。这怎么行?从 Shyju 的示例中,ID 已经预先创建??还是我只是误解了这个例子?
  • 最佳答案

    您可以使用编辑器模板 来处理这个。这是一个工作示例。

    所以我有一个 View 模型来表示父子关系

    public class PersonVM
    {
    public int Id { set; get; }
    public string Name { set; get; }
    public int? ParentId { set; get; }
    public List<PersonVM> Childs { set; get; }
    }

    在我的 GET 操作方法中,我创建了我的 View 模型的对象并将父子数据加载到它。
    public ActionResult EditorTmp(int id = 1)
    {
    //Hard coded for demo, you may replace with actual DB values
    var person = new PersonVM {Id = 1, Name = "Mike"};
    person.Childs = new List<PersonVM>
    {
    new PersonVM {Id = 2, Name = "Scott", ParentId = 11},
    new PersonVM {Id = 2, Name = "Gavin", ParentId = 12}
    };
    return View(person);
    }

    现在我将创建一个 EditorTemplate。为此,请转到您的 Views 文件夹,然后创建一个名为 的目录。编辑器模板 在与 Controller 同名的目录下,添加一个名为 PersonVM.cshtml的 View

    enter image description here

    现在,转到此 View 并添加以下代码。
    @model ReplaceWithYourNameSpaceNameHere.PersonVM
    <div>
    <h4>Childs </h4>
    @Html.TextBoxFor(s => s.Name)
    @Html.HiddenFor(s => s.Id)
    </div>

    现在让我们回到我们的主视图。我们需要将此 View 强类型化为我们原来的 PersonVM。 .我们将在这个 View 中使用 EditorFor html 辅助方法来调用我们的编辑器模板
    @model ReplaceWithYourNameSpaceNameHere.PersonVM
    @using (Html.BeginForm())
    {
    <div>
    @Html.TextBoxFor(s => s.Name)
    @Html.HiddenFor(s => s.Id)
    </div>
    @Html.EditorFor(s=>s.Childs)
    <input type="submit"/>
    }

    现在在 Controller 中有一个 HttpPost 方法来处理表单发布
    [HttpPost]
    public ActionResult EditorTmp(PersonVM model)
    {
    int fatherId = model.Id;
    foreach (var person in model.Childs)
    {
    var id=person.Id;
    var name = person.Name;
    }
    // to do : Save ,then Redirect (PRG pattern)
    return View(model);
    }

    现在,如果您在 HttpPost 操作方法中放置一个断点,您可以看到子项的 Id 被传递到此操作方法。

    enter image description here

    要记住的一件重要事情是,您的编辑器模板 View 的名称应该与您绑定(bind)到它的类型相同。

    关于asp.net-mvc - MVC - 一次性创建对象和相关对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25365043/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com