1

I'm working on a website with 2 languages It's working for me but the problem comes when i change the language of current view which has a parameters , the language of the current view changed and the current view reloaded but without the parameters , it's a null value
how to keep the parameters while changing the language? I searched a lot of time but i didn't find anything could help me

this is what i did

in RouteConfig.cs

  public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");    
            routes.MapRoute(
                 name: "Default",
                 url: "{language}/{controller}/{action}/{id}",
                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, language = "en" }
             );    
        }

in html

@Html.ActionLink(
                 "Arabic",
                 ViewContext.RouteData.Values["action"].ToString(),
                 ViewContext.RouteData.Values["controller"].ToString(),
                 new { language = "ar" }, 
                 null)
  • do you have culture token and some helper ? if so put the culture before the rout new { culture = CultureHelper.GetDefaultCulture(), controller ... – Assaf Our Jan 09 '17 at 13:23
  • no i didn't have culture token – Manar Yousry Jan 09 '17 at 14:52
  • Your issue is likely that you have made your language parameter optional `language = "en"`. For a default language/specified language scenario, there should be a second route (if that is what you are trying to do). If you pass a controller as the first parameter now, it will end up in the language route value. See [this answer](http://stackoverflow.com/a/32839796/181087). – NightOwl888 Jan 19 '17 at 19:11

1 Answers1

0

to change language with "id" attribute in url change:

@Html.ActionLink("Arabic",ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { language = "ar" }, null)

to:

@Html.ActionLink("Arabic",ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { language = "ar", id = ViewContext.RouteData.Values["id"]?.ToString() }, null)
Yosh M.
  • 11
  • 3