代码之家  ›  专栏  ›  技术社区  ›  Debendra Dash

成功调用Jquery Ajay方法后,页面未重定向到视图

  •  0
  • Debendra Dash  · 技术社区  · 6 年前

    我使用jQueryAjax方法从视图调用控制器。控制器操作方法被成功调用,它从数据库检索数据并在相应的视图中显示数据,但最后视图不会生成显示相同视图的方法。

    下面是我调用action方法的Jquery代码。

    <script type="text/javascript">
            $(document).ready(function () {
                $('#btn_Search').click(function (e) {
                    var category = $("#ddl_Category option:selected").text();
                    var location = $('#txtSource').val();
                    $.ajax({
                        url: "/Classified/GlobalSearch",
                        type: 'GET',
                        data: { searchcategory: category, Location: location },
    
                        success: function (data) {
                            alert("Hi");
                        },
                    });
                });
    
         });
    </script>
    

    它调用了这个动作方法。

    public ActionResult GlobalSearch(string searchcategory,string Location)
    {
       //Connect to db and fetch data in form of List
       return View(list);
    }
    

    要检查呼叫是否成功,我已发出hi消息:

    enter image description here

    有人能告诉我需要改变什么吗?

    2 回复  |  直到 6 年前
        1
  •  0
  •   Blackbam    6 年前

    试试这个:

    $.ajax({
    url: "/Classified/GlobalSearch",
    type: 'GET',
    data: { searchcategory: category, Location: location },
    success: function (data) {
        alert("Hi");
        window.location.href = "/page/xyz"; // Try this line after alert.
    },
    });
    
        2
  •  0
  •   Tetsuya Yamamoto    6 年前

    location.href 指定了预期的URL字符串,完全不使用AJAX:

    $(document).ready(function () {
        $('#btn_Search').click(function (e) {
            var category = $("#ddl_Category option:selected").text();
            var location = $('#txtSource').val();
    
            window.location.href = '@Url.Action("GlobalSearch", "Classified")' + '?searchcategory=' + category + '&Location=' + location;
        });
    });
    

    另一种重定向方法是使用普通表单提交(带 POST 方法)和使用 RedirectToAction

    // POST controller action example
    
    string category = "somecategory";
    string location = "somelocation";
    
    return RedirectToAction("GlobalSearch", "Classified", new { searchcategory = category, Location = location });
    

    但是如果您想使用AJAX在同一页面上加载部分视图,只需替换 return View() 具有 return PartialView() :

    public ActionResult GlobalSearch(string searchcategory, string Location)
    {
       //Connect to db and fetch data in form of List
       return PartialView(list);
    }
    

    然后使用 html()

    $('#btn_Search').click(function (e) {
        var category = $("#ddl_Category option:selected").text();
        var location = $('#txtSource').val();
        $.ajax({
            url: "/Classified/GlobalSearch",
            type: 'GET',
            data: { searchcategory: category, Location: location },
    
            success: function (data) {
                alert("Hi");
                $('#targetElement').html(data); // show partial view content to target element
            },
            // other stuff
        });
    });