父子关系的 MVC 路由

发布于 2024-08-16 11:04:22 字数 882 浏览 5 评论 0原文

我正在构建一个 APS.net MVC 2 应用程序,其中有一个父表和一个子表。

我已经为父表构建了控制器、视图和模型,因此我可以添加、编辑、查看详细信息并从父表中删除记录。

我使用以下路由来执行此操作:

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

此设置允许我在我的应用程序中使用以下网址:

列出所有父记录: /Parent/List
查看详细信息 特定的父记录: /父级/详细信息/< ID>
编辑 特定父记录: /父级/编辑/<身份证号>


现在我为每个父记录都有一个子表。标准路由是什么?

列出某个应用程序的子记录 具体父级: 父/< ID>/子项/列表
查看 特定子记录的详细信息: 儿童/详细信息/<身份证号>
编辑 具体子记录:Child/Edit/<身份证号>

看起来合适吗?我该如何设置 MapRoute?

I'm building an APS.net MVC 2 app, where I have a parent table and a child table.

I've got the controller, view and model built for the parent table, so I can Add, Edit, View Details and dete records from the parent table.

I using 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 setup lets me use the following urls in my app:

To list all parent records:
/Parent/List
To view details for
a specific parent record:
/Parent/Details/< ID>
To Edit a
specifc parent record:
/Parent/Edit/< ID>

etc

Now I have a child table for each parent record. What is the standard routing for this?

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>

Does that look right? And how would I go about setting up the MapRoute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や莫失莫忘 2024-08-23 11:04:22

对于列表中的第二项和第三项,您当前的路线会很好,也就是说,如果您有一个子控制器。

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”是单词“parent”而不是名称,那么路线将是

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

,签名将是

public ActionResult ChildList(string id)

For the 2nd and 3rd items in your list you current route will be fine, that is if you have a child controller.

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>

For the 1st item simply create a new route

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

I am assuming you want to use the controller child and will create a new action method called ChildList

The signature would be like this

public ActionResult ChildList(string parent, string id)

Quick point if "Parent" is the word parent rather than a name then the route will be

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

and signature would be

public ActionResult ChildList(string id)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文