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

C# 如何通过部分ASP.NET MVC 4视图在父列表中创建子对象,c#,asp.net,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Razor,在谷歌搜索了一大堆之后,我仍然被这个问题困扰着。我对Asp.NETMVC很陌生 假设您有这些模型类: public class Foo { public int Id { get; set; } public string Name { get; set; } public string SthElse { get; set; } public List BarList { get; set; } }

在谷歌搜索了一大堆之后,我仍然被这个问题困扰着。我对Asp.NETMVC很陌生

假设您有这些模型类:


    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; }
    }
在这里,我使用搭建的createactionresult方法对Foo和Bar使用单独的控制器。在FooCreate视图中,我需要添加一些带有模态形式的条形图对象,并显示添加的条形图对象,如示例所示

仅适用于您以前插入对象的情况(您无法在同一视图中创建艺术家、流派和唱片集,即:想象一个“添加新音乐CD”用例,您需要能够在创建唱片集的同一视图中创建新艺术家、新流派)。如何使用模态视图来实现这一点

你能给我一个教程或者其他介绍这个场景的东西吗(在同一个视图中创建一个新的Foo和Bar子对象)

提前谢谢。

这很容易

模型

控制器

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

    }
}
视图:

@model FooBar
@使用(Html.BeginForm){
@EditorFor(x=>x.Foo)
@编辑器(x=>x.Bar)
}

您需要通过组合这两个模型来创建视图模型,但“创建视图”栏是否需要是局部的?您的意思是要创建选项卡式布局?从您发布的链接中的图片开始?是的,但是对于每个选项卡,我想添加CRUD操作,即:我想能够使用模式窗口表单自由添加/删除/更新“覆盖设备”,就像我们在一些单页应用程序上所做的那样。感谢您回答@erik funkenbusch。但这种情况是父子关系,其中Foo有一个Bar对象列表,而不仅仅是Foo和Bar的组合。问题是:我想在同一页上添加一个新学生,并为该学生分配课程注册。@CezarLamann-您只需在创建或编辑过程中将其添加到列表中即可
public class FooBarController : Controller
{
    public ActionResult Index() {};
    public ActionResult Edit(FooBar model)
    { 

    }
}
@model FooBar

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