Beginner - combining parent and child views in one page

I'm trying to find out mvc - I apologize in advance for all the stupid questions. What I did was created by mvc proejct based on an exsiting database, and then I cheated on it to try to understand what was created for me and how to create my own things. Unfortunately, since I am a new stackoverflow user, I cannot post an image of my project structure.
I have a parent controller and a child controller created using the wizard based on two separate tables that I have in my model. I want to display both of them in one view - ultimately in a webgrid and be able to modify the data for any parent or child. You can ignore the CombinedController. I was engaged in reading, and I found out that I had to create a presentation model that combines both parent and child at the model level, and then from there.

so I created this class:

Imports System.Data.Objects.DataClasses Public Class ParentAndChild Public Property myChildren As IEnumerable(Of Child) Public Property myParent as Parent End Class 

I have a few questions:

Question 1
Should I add this ParentAndChild object to the .edmx file in order to create a controller and view it? I assume that I am really asking if I should first create a view in the sql database, show that the object appeared in .edmx, and then create the controller? Or can I just combine the two objects in the class? This is what I have done so far. I do not have sql View in my database joining these two tables. The reason why I ask is because when I create my controller, if I want to get all CRUD for free, I need to create using the Entity Framework. But I do not know what to specify for the data context.

Question 2
If I want the webgrid to somehow show all my parents and all their children, will the new ParentAndChild class work? I think this will only show details for one parent and his children. It seems to me that I need to create a list of parents .. and then add a set of modules in the parent list for each parent. But I do not know how to do this.

Question 3
How does entityframework know which modules are returned when I use my new class? I donโ€™t define relationships anywhere ... Is it because in the .edmx file the system knows the relationships between the tables?

+4
source share
1 answer

If you just need to access the information of the parent and its child, you should be able to send your view to the parent. A ViewModel is not absolutely necessary if the Entity Framework is aware of the relationship between two objects. If the relationship between the two tables exists at the database level, then the Entity Framework should have automatically modeled it.

An example of a controller might be:

 public ActionResult Parent(int id) { var parent = context.Foo.Single(x => x.Id == id); return View(parent); } 

If you need to create a table of the entire child, you can do something like this:

 @foreach(var item in Model.Children) { <td>@item.Property</td> <td>@item.Property2</td> } 
+1
source

Source: https://habr.com/ru/post/1384389/


All Articles