0

I have an action in controller:

    public ActionResult Close(DocType docType)
    {
        return View();
    }

where DocType is a simple enum. I want to have 2 different links to the same action but with different parameters. I have tried this:

    <mvcSiteMapNode title="Accounting" clickable="false" imageUrl="~/Content/Images/Buttons/MenuButtons/billing.png" visibility="path">
       <mvcSiteMapNode title="Payments" controller="Payment" action="Index"></mvcSiteMapNode>
       <mvcSiteMapNode title="Closing WO" controller="Payment" action="Close" docType="2"></mvcSiteMapNode>
       <mvcSiteMapNode title="Closing WS" controller="Payment" action="Close" docType="4"></mvcSiteMapNode>
   </mvcSiteMapNode>

But in the menu I have 2 links without any parameters: "/Payments/Close"

What's wrong? How to add parameters into mvcSiteMapNode?

Here are my RouteConfig:

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

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
Roman Koliada
  • 4,286
  • 2
  • 30
  • 59

1 Answers1

0

If you want to use your default route, you have to use id for the route key (since it only supports the keys controller, action and id). If you don't, you will get a route with a query string ?docType=2, since this is extra undefined information that is not part of the route.

<mvcSiteMapNode title="Accounting" clickable="false" imageUrl="~/Content/Images/Buttons/MenuButtons/billing.png" visibility="path">
    <mvcSiteMapNode title="Payments" controller="Payment" action="Index"></mvcSiteMapNode>
    <mvcSiteMapNode title="Closing WO" controller="Payment" action="Close" id="2"></mvcSiteMapNode>
    <mvcSiteMapNode title="Closing WS" controller="Payment" action="Close" id="4"></mvcSiteMapNode>
</mvcSiteMapNode>

public ActionResult Close(DocType id)
{
    return View();
}

Otherwise, you need to have a route with the key {docType}. Either way, the key names must match in order to generate URLs correctly (as they need to in MVC when using ActionLink).

NightOwl888
  • 55,572
  • 24
  • 139
  • 212