-1

Base COntroller

public class CultureController : Controller    
{
        public ActionResult SetCulture(string culture)
        {
            HttpContext.Session["culture"] = culture
            return RedirectToAction("Index", "Home");
        }        
}

Action Link in _Layout.cshtml file which is applicable to all the views

<li><%= Html.ActionLink("French", "SetCulture", new {controller = "Culture", culture = "fr-FR"})%></li>
<li><%= Html.ActionLink("Spanish", "SetCulture", new {controller = "Culture", culture = "es-ES"})%></li>

Its working and changing the culture as expected.

But My issue is, it is redirecting to HOme/Index Page whenever I'm changinhg the Language/Culture.

Suppose I'm on the Registration page and I want to change the Culture, it should not redirect me to Home/Index Page, rather, I would like to stay on the same page. How is that possible?

Thanks you guys in advance.

Please help.

Pearl
  • 8,373
  • 8
  • 40
  • 59

2 Answers2

2

you can redirect to the same page. I believe, reloading page would be neccessary, as all resources on your current page need to be updated, based on culture. you can utilize UrlReferrer.AbsoluteUri. do something like:

public ActionResult SetCulture(string culture)
{
        HttpContext.Session["culture"] = culture;
        //all your logic
        return Redirect(Request.UrlReferrer.AbsoluteUri);
}   
Zeeshan
  • 2,884
  • 3
  • 28
  • 47
1

If you want to stay on the same page, use ajax to send the culture value to the controller. This is much more efficient than trying to redirect back to the current page and having to render the view all over again.

In you layout, generate your links as

<a href="#" class="setculture" data-culture="fr-FR">French</a>
<a href="#" class="setculture" data-culture="es-ES">Spanish</a>

Then include a script (include jquery{version}.js in the layout

<script>
  var url = '@Url.Action("SetCulture", "Culture")';
  $('.setculture').click(function() {
    var culture = $(this).data('culture');
    $.post(url, { culture: culture }, function(data) {

    });
  });
</script>

Then decorate your method with [HttpPost] (your changing data so a POST is more appropriate) and remove the return RedirectToAction(); statement.

Note you could also return something from the method such as a success message and display it in the current view if desired.