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

websss
websss used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
PANkaj KumarSoftware Developer

Commented:
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.
websssCEO

Author

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
CEO
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial