0

The issue is that id = 5 is already in the URL as http://localhost:4032/Category/5. But to pass that value to the ActionLink it seems I have to do this:

<td> @Html.ActionLink(p.ProdNo, "Main", "Home", new { id = 5, prodno = p.ProdNo }, null) </td>

Which results in the correct URL: http://localhost:4032/Main/5/1097

But having to do that doesn't seem very right. I know there must be some clever way to handle this. Unfortunately, it's late in the day and I'm all out of clever.

I tried this:

<td> @Html.ActionLink(p.ProdNo, "Main", "Home", new {prodno = p.ProdNo }, null) </td>

But ended up with http://localhost:4032/Main?prodno=1097.

I tried adding the parameters to the corresponding method in the codebehind but that didn't seem to work either.

So in short, when using an ActionLink how do I get the routevalue already in the URL AND pass in another/new routevalue?

Global.asax

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Sections", // Route name
                "{controller}/{action}/{id}/{prodno}/{instid}/{section}", // URL with parameters
                new { controller = "TestEdit", action = "Index", id = UrlParameter.Optional, prodno = UrlParameter.Optional, instid = UrlParameter.Optional, section = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                "Pumps", // Route name
                "{controller}/{action}/{id}/{prodno}", // URL with parameters
                new { controller = "Home", action = "Main", id = UrlParameter.Optional, prodno = UrlParameter.Optional  } // Parameter defaults
            );

            routes.MapRoute(
                "Jobs", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Jobs", id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }
dotnetN00b
  • 5,021
  • 13
  • 62
  • 95

2 Answers2

1

I think you need to set up the route for your prodno-parameter. Looks like this thread could contain a solution:

Routing with Multiple Parameters using ASP.NET MVC

Community
  • 1
  • 1
1

You could fetch it from the RouteData:

@Html.ActionLink(
    p.ProdNo, 
    "Main", 
    "Home", 
    new { id = ViewContext.RouteData["id"], prodno = p.ProdNo }, 
    null
)
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928