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

我想在同一视图中创建一个带有子/相关对象的父对象.
一个例子是:创建一个父亲(带有一些名字)以及他的所有儿子(带有他们的名字).我创建了一个视图模型:
public class Fatherviewmodel {
  public Father father {get; set;} // has 1 property Name
  public List<Son> {get; set;} // has 1 property Name
}

我的问题是,如何在发布帖子时从视图中获取Sons列表?
我已经尝试为每个Son id使用HiddenFor,但无论如何,当返回到控制器时列表为空.

更新:

我尝试了下面描述的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; }
}

我这样做了:

>为具有索引,创建,编辑的人员提供完整控制器的脚手架…
>在Views-> Person中创建了EditorTemplates文件
>创建Person.cshtml:

@model TestEditorTemplate.Models.Person
< DIV>
< H4>儿童< / H4>
@ Html.TextBoxFor(s => s.Name)
@ Html.HiddenFor(s => s.Id)
< / DIV>
>将@ Html.EditorFor(m => m.Children)添加到Create.cshtml

问题:

> @ Html.EditorFor(m => m.Children)如何使用
当m.Children是Person的集合而不是单个集合时的编辑器模板
人?
>我想同时创建(不编辑)包括孩子在内的父亲.这意味着我没有ID传递到Create视图开始.这怎么办?从Shyju的例子来看,Ids已经预先创建了?或者我只是误解了这个例子?

解决方法

您可以使用EditorTemplates来处理此问题.这是一个工作样本.

所以我有一个viewmodel来表示父子关系

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动作方法中,我创建了一个视图模型的对象,并将Father -childs数据加载到它.

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文件夹,并在与控制器同名的目录下创建名为EditorTemplates的目录,并添加名为PersonVM.cshtml的视图

现在,转到此视图并添加以下代码.

@model ReplaceWithYourNameSpaceNameHere.PersonVM
<div>
    <h4>Childs </h4>
    @Html.TextBoxFor(s => s.Name)
    @Html.HiddenFor(s => s.Id)
</div>

现在让我们回到主视图.我们需要将此视图强类型化为我们原始的PersonVM.我们将在此视图中使用EditorFor html helper方法调用我们的编辑器模板

@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"/>
}

现在在控制器中有一个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被传递给此操作方法.

需要记住的一件重要事情是,您的编辑器模板视图的名称应与您绑定它的类型相同.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....