Pergunta

I have a nav menu with links structured like so:

<div class="childLinkGroup">
    <div class="headerLink">@Html.ActionLink("Engine Products", null, "EngineProducts", null, null)</div>
    <ul>
        <li>@Html.ActionLink("Perkins Engines", "PerkinsEngines", "EngineProducts", null, null)</li>
        <li>@Html.ActionLink("Isuzu Engines", "IsuzuEngines", "EngineProducts", null, null)</li>
        <li>@Html.ActionLink("FPT PowerTrain", "FPTPowerTrain", "EngineProducts", null, null)</li>
        <li>@Html.ActionLink("Mitsubishi Engines", "MitsubishiEngines", "EngineProducts", null, null)</li>
    </ul>
</div>

And in the controller class:

// GET: /EngineProducts/
public ActionResult Index()
{
    return View(ModelData);
}

//
// GET: /EngineProducts/PerkinsEngines
public ActionResult PerkinsEngines()
{
    ModelData.MenuCategories.Where(x => x.Action == "PerkinsEngines").FirstOrDefault().Active = true;

    return View("Perkins Engines", ModelData);
}

These all work fine when accessed from the homepage.

If you're already on a child page like /EngineProducts/IsuzuEngines and try to access a different parent level link like /TransmissionProducts the IsuzuEngines is left on the link resulting in a page that can't be found.

Desired url: /TransmissionProducts

Actual url: /TransmissionProducts/IsuzuEngines

There's a bunch of overrides for Html.ActionLink and while I looked though them I don't see a different set of params that looks better.

Foi útil?

Solução

When your parent links are constructed like this:

@Html.ActionLink("Engine Products", null, "EngineProducts", null, null)

The previous request value for action will be used because you are providing null when constructing the link. You should explicitly specify the action that will be used when a request is made for a parent link.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top