CRUD operations for multiple related records in different tables from single view in MVC C#

asp.net-mvc asp.net-mvc-5 c# entity-framework-6

Popular Answer

I can tell that you want to create a list of children for a certain parent, but it doesn't seem that way from your viewmodel.

using MyApp.Models;
namespace MyApp.ViewModels
{
  public class ParentChildViewModel
    {
      public int parent_id { get; set; }
      public string parent_name { get; set; }
      public string parent_address { get; set; }
      public string child_name { get; set; }
      public string child_allergies { get; set; }
    }
}

A viewmodel with a list of children would be useful. Possibly like this:

using MyApp.Models;
namespace MyApp.ViewModels
{
  public class ParentChildViewModel
    {
      public int parent_id { get; set; }
      public string parent_name { get; set; }
      public string parent_address { get; set; }
      public IEnumerable<Child> children { get; set; }
    }
}

Here, I'm assuming that all you want to do is repeat the list. I don't utilise the IList interface; instead, I use the IEnumerable interface.

Calling the "Create" method of your controller and passing the new child's parent id will add a child to the list. Therefore, utilising your application context in your controller, you may add a new child for a certain parent in the database.

You may construct a new ParentChildViewModel, populate it with the appropriate children, and then return it to the view after the database transaction is complete.

0
7/29/2017 3:14:06 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow