Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging single route parameter in ActionLink
    text
    copied!<p>I am trying to implement a language chooser which is visible on all pages.</p> <p>My application currently has two routes:</p> <pre><code>routes.MapRoute( name: "EventDriven", url: "{language}/{eventid}/{controller}/{action}/{id}", defaults: new { language = "en", controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( name: "Default", url: "{language}/{controller}/{action}/{id}", defaults: new { language = "en", controller = "Home", action = "Index", id = UrlParameter.Optional } ); </code></pre> <p>And in my shared <code>_layout.cshtml</code> file I have the following action links:</p> <pre><code>@Html.ActionLink("English", ViewContext.RouteData.Values["action"].ToString(), new {language="en"}) @Html.ActionLink("Français", ViewContext.RouteData.Values["action"].ToString(), new {language="fr"}) </code></pre> <p>The problem I am encountering is I want the <code>{eventid}</code> route segment preserved, but it's not applicable on every url.</p> <p>On the home index page <code>http://localhost/MySite/</code>, the two action links are as follows:</p> <pre><code>English: http://localhost/MySite/ French: http://localhost/MySite/fr </code></pre> <p>Which is good, but on my interior page <code>http://localhost/MySite/en/2/Donation</code> the action links are:</p> <pre><code>English: http://localhost/MySite/en/2/Donation French: http://localhost/MySite/fr/Donation </code></pre> <p>If I go to <code>http://localhost/MySite/fr/2/Donation</code> then the action links are:</p> <pre><code>English: http://localhost/MySite/en/Donation French: http://localhost/MySite/fr/2/Donation </code></pre> <p>The problem is the change language action link does not contain the eventid <code>2</code> information.</p> <p>How do I make it so both links contain the event and language information (and any other route parameters unforeseeable in the future) without having to program explicitly for them?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload