简体   繁体   English

如何通过部分ASP.NET MVC 4视图在父列表内创建子对象

[英]How to create child objects inside parent list through partial ASP.NET MVC 4 views

After Googling a bunch, I'm still stuck on this problem. 搜寻了一堆之后,我仍然停留在这个问题上。 I'm pretty new on Asp.Net MVC things 我对Asp.Net MVC东西很陌生

Say you have those model classes: 假设您有那些模型类:


    public class Foo
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string SthElse { get; set; }
        public List BarList { get; set; }
    }

    public class Bar
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string SthElse { get; set; }
        public Foo ParentFoo { get; set; }
    }

Here I have individual controllers to Foo and Bar with a scaffolded Create ActionResult method on both. 在这里,我对Foo和Bar都有单独的控制器,并在两者上都使用了脚手架的Create ActionResult方法。 On the Foo Create view, I need to add some Bar objects with a modal form and show added Bar objects like on this example. 在Foo Create视图上,我需要以模态形式添加一些Bar对象,并像示例一样显示添加的Bar对象。

The ASP.NET Music Store Example only covers the case where you have previously inserted objects (you couldn't create Artist, Genre and Album at the same view, IE: Imagine an "Add new Music CD" use-case where you need to be able to create a new Artist, a new Genre at the same view where you create an Album). ASP.NET音乐商店示例”仅涵盖您先前插入对象的情况(您无法在同一视图上创建艺术家,风格和专辑,即IE:想象一个“添加新音乐CD”用例,您需要在能够在创建专辑的同一视图上创建新的艺术家和流派)。 How do I could accomplish that using modal views? 如何使用模态视图来实现?

Could you point me a tutorial or else that covers this scenario (Create a new Foo and Bar children at the same view)? 您能给我指个教程还是涵盖这种情况(在同一视图中创建一个新的Foo和Bar子级)?

Thank you in advance. 先感谢您。

This is easy. 这很容易。

Model 模型

public class FooBar {
    public Foo Foo {get;set;}
    public Bar Bar {get;set:}
}

Controller 调节器

public class FooBarController : Controller
{
    public ActionResult Index() {};
    public ActionResult Edit(FooBar model)
    { 

    }
}

View: 视图:

@model FooBar

@using(Html.BeginForm) {
    @EditorFor(x => x.Foo)
    @EditorFor(x => x.Bar)
    <input type="submit"/>
}

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

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