【问题标题】:Asp.Net Core MVC EF Model with List of Entities Create in SingleView具有在 SingleView 中创建的实体列表的 Asp.Net Core MVC EF 模型
【发布时间】:2020-08-09 22:45:26
【问题描述】:

您好,我在 ASP.NET Core 3.1 中使用 MVC 模式的实体框架有以下模型:

public class Parent
{
   int ParentId {get; set;}
   
   string name {get; set;}
   
   ICollection<Child> childs {get; set;}
}
 
public class Child
{
   int ChildId {get; set;}

   string name{get;set;}
}

我想要父类的 CRUD 视图,我可以在其中动态添加和删除子类,然后通过提交将其发送到控制器并将父类和子类映射到 DBContext。 就像在购物车中一样,您可以在其中添加商品。 使用 Ajax 调用的孩子的部分视图是最好的方法吗? 任何建议将不胜感激。对于像我这样的初学者来说,如此复杂的任务很难。

【问题讨论】:

    标签: c# jquery entity-framework asp.net-core-mvc


    【解决方案1】:

    a partial View for child that gets called with Ajax 是个好主意。使用局部视图,您可以重复使用它。而使用ajax,您只需将单个子级传递给控制器​​,而无需像表单提交那样刷新页面。

    我想也许你可以这样改变 Child:

    public class Child
    {
       int ParentId {get; set;}
    
       int ChildId {get; set;}
    
       string name{get;set;}
    }
    

    并这样做以添加局部视图:

    @await Html.PartialAsync("_Partial", @Model.Childs)
    

    然后您可以使用 ajax 在局部视图中添加子项,当单击按钮将新的子项内容传递给控制器​​时。您需要传递 ParentId 以便您可以将子项添加到父模型的子项中。

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 2021-05-26
      • 2017-07-11
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      相关资源
      最近更新 更多