MVC routing for parent child relationships

I am creating an APS.net MVC 2 application where I have a parent table and a child table.

I have a controller, view and model built for the parent table, so I can add, edit, view details and write records from the parent table.

I use the following routing to do this:

routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Parent", action = "List", id = "" }  // Parameter defaults

This setting allows you to use the following URLs in my application:

To list all parent entries: / Parent / List
To view details for a specific parent entry: / Parent / Details / <ID>
Change to specify parent entry: / Parent / Edit / <ID>

etc.

. ?

: /<ID> //
: //<ID>
: Child/Edit/<ID>

? MapRoute?

+3
1

2- 3- , .

To list app child records for a specific parent: Parent/< ID>/Child/List 
To view details for a specific child record: Child/Details/< ID> 
To edit a specific child record: Child/Edit/< ID>

routes.MapRoute(
 "ParentWithChild", 
 "{parent}/{id}/Child/List", 
 new { controller = "Child", action = "ChildList", id = "", parent=""} 

, ChildList

:

public ActionResult ChildList(string parent, string id)

, "" - , ,

 "Parent/{id}/Child/List",

public ActionResult ChildList(string id)
+4

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


All Articles