What I want to do is have a page at /Products/Details/{id} which routes to the action details on ProductsController, and then an edit version of the page at /Products/Details/Edit/{id}. I tried to do this using [ActionName("Details/Edit")] on the Action but that doesnt work.
+4
A:
You can't have a slash in your action name.
Why not have the following actions?
- /Products/Details/{id} -For display
- /Products/Edit/{id} -For edit
My preference would be to do the following:
- /Products/{id}/View -For display
- /Products/{id}/Edit/ -For edit
Hope that makes sense!
Lewis
2009-08-12 09:33:13
because each product has a number of views. Details, history, documents etc, Each of these pages is to be presented in a view and edit form.
Jack Ryan
2009-08-12 09:34:04
However I do like the routing pattern you describe. /Products/{id}/Details and /Products/{id}/Details/Edit would be perfectly acceptable.
Jack Ryan
2009-08-12 09:35:11
I would go even further and just remove the /Details/ for viewing a product i.e. /products/{id} and editing could be /products/edit/{id}
David Liddle
2009-08-12 12:14:10
+1
A:
Add route like this BEFORE the default one:
routes.MapRoute(
"DefaultWithDetails",
"{controller}/Details/{action}/{id}"},
null);
Dmytrii Nagirniak
2009-08-12 10:05:14
+1 to do what JayArr requested, though I think that semantically Lewis' suggestion is a better idea.
James S
2009-08-12 18:19:28
A:
What you can do is this. Setup a new route like this before the default route;
routes.MapRoute(
"PRoute",
"{controller}/{action}/{action2}/{id}",
null
);
then in your Products controller have your action like this, notice that the parameter names match the names in the route.
public ActionResult Details(string action2, string id)
{
switch (action2)
{
case "edit":
// Do Something.
return View("edit");
case "view":
// Do Something.
return View("view");
default :
// Do Something.
return View("bad-action-error");
}
}
Now the Details action will be passed action2 and the id from the url. So a URL like /products/details/view/7 the details action will get "view" and "7" , then you can use a switch or if statement on action2 to continue your processing. This can now easily be expanded to include other sub-actions.
Tony Borf
2009-08-13 13:56:25