1

I have the following code which works on the first time around:

$("#CompDD").change(function () {
                //var parts = (window.location.pathname.split("/"));
                var ctrlName = '@ViewContext.RouteData.Values["Controller"].ToString()';
                var actnName = '@ViewContext.RouteData.Values["Action"].ToString()';
                var url = (ctrlName + "/" + actnName + "/?SearchString=" + $('#CompDD option:selected').text() + "&atton=" + $('#AttDD option:selected').val());
                //delete ctrlName;
               // delete actnName;
                //window.location = ($(location).attr('href') + "/?SearchString=" + $('#CompDD option:selected').text() + "&atton=" + $('#AttDD option:selected').val());
                //window.location = (ctrlName + "/" + actnName + "/?SearchString=" + $('#CompDD option:selected').text() + "&atton=" + $('#AttDD option:selected').val());
                //$(location).attr('href', url);
                window.location.href = url;
                //alert(ctrlName + "\n" + actnName);
            });

However on subsequent changes of the drop down in question (#CompDD) it will add another controller/action to the end of the link, ex. it adds another "Patrons/Index" to the end of the existing "Patrons/Index", thenit adds the searchsrting variables etc.

Please excuse the comments and stuff on my code. How do i get Jquery (or javascript) to redirect without appending the controller name and action names over and over, or whats the best way to do this?

EDIT: Such an easy fix! I had to add the root slash to the URL string, example this worked:

var url = ("/" + ctrlName + "/" + actnName + "/?SearchString=" + $('#CompDD option:selected').text() + "&atton=" + $('#AttDD option:selected').val());

Notice the forward slash at the start of the string I construct....Yikes!

3
  • You could just set the querystring part with window.location.search = "?SearchString=something" etc. Commented Oct 17, 2015 at 22:42
  • Why not just put the dropdownlist inside a form with FormMethod.Get and submit the form? Commented Oct 18, 2015 at 2:59
  • adeno, how would you use the window.location.search? Commented Oct 20, 2015 at 21:45

2 Answers 2

5

Use the Url.Action helper method to build path to action methods.

$("#CompDD").change(function () {

   var baseUrl="@Url.Action("Home","Search")";
   alert(baseUrl);
   // Now append your query string variables to baseUrl
   // Ex : baseUrl=baseUrl+"?searchString=testing"; 
   window.location.href=baseUrl;

});

Assuming you want to navigate to the search action method in Home controller.

Sign up to request clarification or add additional context in comments.

1 Comment

My problem is the controller stays the same but the action is different depending on what index view (index1, index2, or index3) they are looking at. I could re-write the code for each index view but i wanted to save code and have the function on the master page.
0
 function RedirectUrl() {
        if (domElement.textfor.val() == "Index") {
            window. location.href = E_host.AppVar.AppHost + "/Home/index";
        }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.