Select OnChange - redirect to current page with querystring parameter appended to url

I have a layout.cshtml which all pages use

I have a HTML SELECT on this layout
When someone changes the drop down, it fires this code

   $('#sitesDdlLayout').on('change', function () {

                        var selectedSiteId = $("#sitesDdlLayout option:selected").val();

                         $.ajax({
                            type: "POST",
                             url: "@Url.Action("RedirectToSiteById", "Home")",
                            data: "{'SiteId':'" + selectedSiteId + "'}",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (response) {

                                window.location.href = response.Url;

                            }
                         });
                });

Open in new window


Then it fires this
   [HttpPost]
        public ActionResult RedirectToSiteById(string SiteId)
        {
            var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index", "Home", new { SiteId = SiteId });
            return Json(new { Url = redirectUrl });
        }

Open in new window


This works fine, apart from always redirecting to the INDEX page

What I want to do is to reload the current page, and pass in query string of SITEID into the URL which will then pull in the correct content based on the site

i,e, something like this
 
       $('#sitesDdlLayout').on('change', function () {

                        var selectedSiteId = $("#sitesDdlLayout option:selected").val();
                      ....
                        url: "@Url.Action("CURRENTPAGE", "Home", URLPARAMETERHERE)",
                      ....

Open in new window


the idea is the user stays on same page,  but its reloaded with the correct SiteId appended to the URL
Any ideas the best way to do this inside the layout.cshtml file?
websssCEOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PANkaj KumarSoftware DeveloperCommented:
Hi Websss,

With partial view you can load data or page in layout.cshtml without reloading page.

Do you want to change url also as after #sitesDdlLayout dropdown change without reloading page.
websssCEOAuthor Commented:
Yes I want to refresh the whole page, but the Url would be the same with the parameter
i.e.
mysite.com/home/apage
to
mysite.com/home/apage?siteid=3
websssCEOAuthor Commented:
                var controller = "@ViewContext.RouteData.Values["controller"].ToString()";
                var action = "@ViewContext.RouteData.Values["action"].ToString()";



                 $('#sitesDdlLayout').on('change', function () {

                     var selectedSiteId = $("#sitesDdlLayout option:selected").val();

                         $.ajax({
                            type: "POST",
                             url: "@Url.Action("RedirectToSiteById", "Home")",
                             data: "{'SiteId':'" + selectedSiteId + "'  ,'Controller':'" + controller + "','Action': '" + action  + "'}",

                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (response) {

                                window.location.href = response.Url;

                            }
                         });
                        
                });

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET MVC

From novice to tech pro — start learning today.