MVC4 ActionLink not passing parameters

first MVC4 project and I've hit the buffers...

I want to be able to reference my site pages thus;

MENU/SUBMENU/DOCUMENT/ID

where the ID value is optional and anything else tagged on the end, the Model will process the Document part which will be as text, e.g.,

HOME/WELCOME/Welcome-to-our-store

So I have a route set up (there's only 1 route)

routes.MapRoute(
    name: "Default",
    url: "{menu}/{submenu}/{document}/{id}",
    defaults: new {
        controller = "Page", 
        action= "Data", 
        menu = "",
        submenu = "",
        document = "", 
        id = ""}

I have a controller;

public class PageController : Controller
{

    // Default view option for documents


    public ActionResult Data(string menu, string submenu, string document, string parameters)
    {


        // Set the View to use rather than it default;
        string thisView = "Data";

        return View(thisView);
    }

}

We have a _Layout.cshtml file that references a _MenuPartial.cshtml file

@model MyNameSpace.Models.PageModel
<ul>
    @foreach (var item in Model.Menus.MainMenu)
    {
        <li>@Html.ActionLink(item.Label, "Page", "Data", new
           {
               menu = "MENU",
               submenu = "SUBMENU"
           },
           null
           )
        </li>
    }
</ul>

The above all writes out my main menu dynamically with the correct text etc., etc.

However, when I click on a label, the route seems to map through happily to the PageControllers Data function but the values of string menu, string submenu, string document, string parameters are "" or null.

If I modify the URL then they get passed in correctly.

I'm sure that this is just a misunderstanding of what I should be doing and I hope once some kind soul has spotted it I'll be off and running !

thanks Andy

Upvotes: 0

Views: 511

Answers (1)

Jesper
Jesper

Reputation: 7605

Try giving the document and id defaults the value UrlParameter.Optional in the route and give the document and id parameters in the action the default value "" (string document = ""). That should allow routing to reconcile the action link route data with the route.

Upvotes: 2

Related Questions