最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Jquery or Javascript Redirect to ControllerAction - Stack Overflow

programmeradmin2浏览0评论

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 ments 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!

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 ments 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!

Share Improve this question edited Jan 1, 2016 at 14:26 tereško 58.5k25 gold badges100 silver badges150 bronze badges asked Oct 17, 2015 at 22:30 dave317dave317 7742 gold badges12 silver badges33 bronze badges 3
  • You could just set the querystring part with window.location.search = "?SearchString=something" etc. – adeneo Commented Oct 17, 2015 at 22:42
  • Why not just put the dropdownlist inside a form with FormMethod.Get and submit the form? – user3559349 Commented Oct 18, 2015 at 2:59
  • adeno, how would you use the window.location.search? – dave317 Commented Oct 20, 2015 at 21:45
Add a ment  | 

2 Answers 2

Reset to default 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.

 function RedirectUrl() {
        if (domElement.textfor.val() == "Index") {
            window. location.href = E_host.AppVar.AppHost + "/Home/index";
        }
}
发布评论

评论列表(0)

  1. 暂无评论