简体   繁体   English

如何使用ASP.Core和EF为多级子模型创建CRUD?

[英]How to create a CRUD for multi-level child Model with ASP.Core & EF?

I'm trying to create a CRUD that can edit a model and all its children, but each time the parent model ends up empty, how to properly do a scaffolded CRUD with ASP.Core 2.2 ? 我正在尝试创建一个可以编辑模型及其所有子模型的CRUD,但是每次父模型最终为空时, 如何使用ASP.Core 2.2正确地制作脚手架CRUD

//Models

class Book {
    int IdBook {get; set;}
    string Name {getl set;}
    ICollection<Page> PageList {get; set;}
}

class Page {
    int IdPage {get; set;}
    string Name {get; set;}
    ICollection<Line> LineList {get; set;}
}

class Line{
     int IdLine {get;set;}
     string Content {get; set;} 
}

Here's my controller 这是我的控制器

//Controller

public async Task<IActionResult> Edit(int? id)
{
    var book = _context.Book
                    .Include(b => b.PageList)
                    .ThenInclude(p => p.LineList)
                    .First();

    return View(book);
}

Here's what I'm trying to do 这就是我想要做的

@model Book

@Model.Name
@for(var indexPage = 0; indexPage < Model.PageList.Count; indexPage++)
{
    @Model.PageList[indexPage].Name
    @for(var indexLine = 0; indexLine < Model.PageList[indexPage].LineList.Count)
    {
        Html.EditorFor(x => x.PageList[indexPage].LineList[indexLine].Content)
    }
}

But when I post my form, I only get the properties of Book , and Book.PageList is null , what is the proper way do that? 但是,当我发布表单时,我仅获得Book的属性,而Book.PageListnull ,这样做的正确方法是什么? Is there any tutorial I would have missed? 我会错过任何教程吗?

UPDATE UPDATE

The problem seems to be the type, the controller receives the post parameter (My code is a bit different, but the same, the books were an example) 问题似乎是类型,控制器接收了post参数(我的代码有点不同,但是相同,书籍只是一个例子)

窃听器

Can you post entire code for the action method on the controller that processes your request? 您可以在处理您的请求的控制器上发布动作方法的完整代码吗?

From your code on the razor view page, in the inner loop where you are iterating through PageList, your are not incrementing your indexLine . 在razor视图页面上的代码中,在要遍历indexLine的内部循环中,您不会递增indexLine Shouldn't this line 这行不应该

@for(var indexLine = 0; indexLine < Model.PageList[indexPage].LineList.Count) be @for(var indexLine = 0; indexLine < Model.PageList[indexPage].LineList.Count)

@for(var indexLine = 0; indexLine < Model.PageList[indexPage].LineList.Count, indexLine++) ? @for(var indexLine = 0; indexLine < Model.PageList[indexPage].LineList.Count, indexLine++)吗?

Again, On the controller, if the Request.Form property has the entire 'supposed' payload but model binding isn't working, try annotating the Submission parameter with [FromBody] Annotation to clearly inform ASP.NET to bind Submission from bofy of the request - like so 再次,在控制器上,如果Request.Form属性具有整个“假定的”有效负载,但模型绑定不起作用,请尝试使用[FromBody]注释对Submission参数进行注释,以清楚地通知ASP.NET绑定Bofy的Submission 。请求-像这样

public async Task<IActionResult> Edit(int id, [FromBody] Submission submission) {}

Look through these tiny fixes and let me know if you still have any issues 查看这些小的修复程序,让我知道您是否还有任何问题

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

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