views:

148

answers:

3

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
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
However I do like the routing pattern you describe. /Products/{id}/Details and /Products/{id}/Details/Edit would be perfectly acceptable.
Jack Ryan
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
+1  A: 

Add route like this BEFORE the default one:

routes.MapRoute(
    "DefaultWithDetails",
    "{controller}/Details/{action}/{id}"},
    null);
Dmytrii Nagirniak
+1 to do what JayArr requested, though I think that semantically Lewis' suggestion is a better idea.
James S
James, yes. I agree. But I just was answering the question :)
Dmytrii Nagirniak
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