Link to home
Create AccountLog in
Avatar of RTSol
RTSol

asked on

returning a view with ajax

Hi,

I have this problem: I have an ajax call to a controller like this:

            $('.flagsdiv').click(function () {
                var id = $(this).attr('id');
                console.log(id);
                var flag = document.getElementById('callus');
                var lang = id;
                var url = '/Home/SetCulture' + '?id=' + lang;
                flag.src = "../images/" + lang + ".gif";
                console.log(url);
                $.ajax({
                    type: "GET",
                    url: url,
                    data: {},
                    dataType: "text"
                });
                document.getElementById("contbox").style.display = "none";
            });

Open in new window


The controller action is like this:

        public ActionResult SetCulture(string id)
        {
            string culture = id;
            culture = CultureHelper.GetImplementedCulture(culture);
            RouteData.Values["culture"] = culture;

            // Save culture in a cookie
            HttpCookie cookie = Request.Cookies["_culture"];
            if (cookie != null)
                cookie.Value = culture;   // update cookie value
            else
            {

                cookie = new HttpCookie("_culture");
                cookie.Value = culture;
                cookie.Expires = DateTime.Now.AddYears(1);
            }
            Response.Cookies.Add(cookie);

            return RedirectToAction("Index", "Home");
        }

Open in new window


This executes this code in the controller

        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to Rt solutions!";
            string culture = CultureHelper.GetCurrentCulture().Substring(0, 2);
            ViewBag.Lang = "../images/" + culture + ".gif";

            return View();
        }

Open in new window


but the Index view is not returned as it should.

What can be the problem?

Best regards
RTSOL
ASKER CERTIFIED SOLUTION
Avatar of Najam Uddin
Najam Uddin
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of RTSol
RTSol

ASKER

Thanks for the hint.

I changed my aiax a bit:
                $.ajax({
                    type: "GET",
                    url: url,
                    data: {},
                    dataType: "text",
                    success: function(data) {
                        document.getElementById("contbox").style.display = "none";
                        window.location.href = window.location;
                    }
                });

Open in new window


and now it works fine.

Thanks