MVC - 一气呵成创建对象和相关对象 [英] MVC - Create object and related objects in one go

查看:175
本文介绍了MVC - 一气呵成创建对象和相关对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 公共类FatherViewModel {
  公共父亲的父亲{搞定;设置;} //有1个属性Name
  公开名单<男孩儿> {得到;设置;} //有1个属性Name
}

我的问题是,我怎么儿子从视图后面的名单进行帖子的时候?
我一直在使用HiddenFor每个子ID尝试过,但无论何时返回到控制器的东西,列表是空的。

更新:

我试过下面描述的编辑模板例如通过Shyju,但我的编辑不会被调用。
我有1对象:

 公共类Person
{
    公众诠释标识{搞定;组; }
    公共字符串名称{;组; }
    公众诠释? FatherId {搞定;组; }
    公共虚拟的ICollection<&人GT;儿童{搞定;组; }
}

我这样做:


  1. 脚手架全控制器人与索引,创建,编辑...

  2. 在视图 - >个人创建的文件夹EditorTemplates

  3. 创建Person.cshtml:

    @model TestEditorTemplate.Models.Person
    < D​​IV>
        < H4>子< / H4>
        @ Html.TextBoxFor(S = GT; s.Name)
        @ Html.HiddenFor(S = GT; s.Id)
    < / DIV>


  4. 新增 @ Html.EditorFor(M = GT; m.Children)来Create.cshtml


问题:


  1. 如何 @ Html.EditorFor(M = GT; m.Children)可能与工作
    m.Children 人的集合而不是一个单一的编辑模板

  2. 我要创建(不能编辑),包括在同一时间孩子的父亲。这意味着,我没有标识传递给创建视图开始。怎么能这样工作?从由Shyju的例子中,ID是已经预先创建??还是我刚才误会的例子?


解决方案

您可以使用 EditorTemplates 以处理这个问题。这里是工作示例。

所以我有一个视图模型重新present父亲与子女的关系。

 公共类PersonVM
{
    公众诠释标识{设置;得到; }
    公共字符串名称{设置;得到; }
    公众诠释? {的ParentId设置;得到; }
    公开名单< PersonVM>蔡尔兹{设置;得到; }
}

在我的GET操作方法,创建我的视图模型的对象与父-childs数据加载到它。

 公众的ActionResult EditorTmp(INT ID = 1)
{
    为演示//硬codeD,您可以用实际的DB值替换
    变种人=新PersonVM {ID = 1,名称=迈克};
    person.Childs =新的List< PersonVM>
    {
        新PersonVM {ID = 2,名称=斯科特的ParentId = 11},
        新PersonVM {ID = 2,名称=加文的ParentId = 12}
    };
    返回查看(人);
}

现在我将创建一个EditorTemplate。要做到这一点,转到您的浏览文件夹,并创建一个名为 EditorTemplates 具有相同的名称作为控制器,并添加一个名为视图中的目录 PersonVM.cshtml

现在,去这个观点,并添加下面的code。

  @model ReplaceWithYourNameSpaceNameHere.PersonVM
< D​​IV>
    < H4>童车< / H4>
    @ Html.TextBoxFor(S = GT; s.Name)
    @ Html.HiddenFor(S = GT; s.Id)
< / DIV>

现在让我们回到我们的主要观点。我们需要让这种观点强类型我们原来的 PersonVM 。我们将使用HTML EditorFor辅助方法,在此视图中拨打我们的编辑模板

  @model ReplaceWithYourNameSpaceNameHere.PersonVM
@using(Html.BeginForm())
{
    < D​​IV>
        @ Html.TextBoxFor(S = GT; s.Name)
        @ Html.HiddenFor(S = GT; s.Id)
    < / DIV>
    @ Html.EditorFor(S = GT; s.Childs)
   <输入类型=提交/>
}

现在已经在控制器的HttpPost方法来处理表单提交

  [HttpPost]
公众的ActionResult EditorTmp(PersonVM模型)
{
    INT fatherId = model.Id;
    的foreach(VAR人model.Childs)
    {
        VAR ID = person.Id;
        变量名称= person.Name;
    }
    //做:保存,然后重定向(PRG模式)
    返回查看(模型);
}

现在,如果你把一个破发点中的HttpPost操作方法,你可以看到孩子的标识中的传递给这个操作方法。

要记住的一件重要的事情是,你的编辑模板视图的名字应该是一样的,你绑定到它的类型。

I want to create a parent object with child/related objects in the same view. An example would be: create one Father (with some name) along with all his sons (with their names). I have created a view model:

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

My question is, how do I get the list of Sons back from the view when the post is performed? I have tried using HiddenFor for each Son id, but no matter what, the list is empty when returned to the controller.

UPDATE:

I tried the Editor Template example by Shyju described below, but my editor is never called. I have 1 object:

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; }
}

I did this:

  1. Scaffolded a full controller for Person with index, create, edit...
  2. Created EditorTemplates folder in Views->Person
  3. Created Person.cshtml:

    @model TestEditorTemplate.Models.Person <div> <h4>Child</h4> @Html.TextBoxFor(s => s.Name) @Html.HiddenFor(s => s.Id) </div>

  4. Added @Html.EditorFor(m => m.Children) to Create.cshtml

Questions:

  1. How can @Html.EditorFor(m => m.Children)possibly work with the editor template when m.Children is a collection of Person and not a single Person?
  2. I want to create (not edit) a father including children at the same time. That means that I have no Ids to pass to the Create view to start with. How can this work? From the example by Shyju, the Ids are already created beforehand?? Or did I just misunderstand the example?

解决方案

You can use EditorTemplates to handle this. Here is a working sample.

So i have a viewmodel to represent the father-child relationship

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

And in my GET action method, i create an object of my view model and load the Father -childs data to it.

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

Now i will create an EditorTemplate. To do that, Go to your Views folder, and Create a directory called EditorTemplates under the directory which has same name as the controller, and add a view with name PersonVM.cshtml

Now, go to this view and add the below code.

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

Now let's go back to our main view. We need to make this view strongly typed to our original PersonVM. We will use the EditorFor html helper method in this view to call our editor template

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

Now have an HttpPost method in the controller to handle the form posting

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

Now, If you put a break point in your HttpPost action method, you can see the Id's of childs are passed to this action method.

One important thing to remember is, Your Editor Template view's name should be same as the type you are binding to it.

这篇关于MVC - 一气呵成创建对象和相关对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆