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
ASP.NET.NET ProgrammingAJAX

Avatar of undefined
Last Comment
RTSol

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Najam Uddin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes