【问题标题】:Page is not redirecting to the view after successful call of a Jquery Ajay method成功调用 Jquery Ajay 方法后,页面未重定向到视图
【发布时间】:2019-05-12 17:52:07
【问题描述】:

我正在使用 Jquery ajax 方法从视图中调用控制器。控制器操作方法被成功调用,它从数据库中检索数据并在各自的视图中显示数据,但最后视图并没有生成它显示相同的视图。

这是我调用操作方法的 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);
}

最后,数据也在全局搜索视图中设置。但是视图没有出现。

为了检查通话是否成功,我发了一条问候信息:

谁能告诉我需要改变什么?

【问题讨论】:

  • 我觉得这个帖子Return view after ajax post to controller可能对你有帮助。
  • 不,它会抛出错误,因为我传递的数据将为空。
  • 如果你想刷新页面,使用ajax调用真的没有意义。如果您不想想要刷新页面,但返回视图内容,您应该使用return PartialView();
  • 是您没有返回数据的问题吗?或者,它是重定向吗?从您的问题中并不清楚实际问题是什么。你能澄清一下吗?
  • 问题在于它没有重定向到 Globalsearch 视图。

标签: jquery ajax asp.net-mvc


【解决方案1】:

试试这个:

$.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】:

正如@HereticMonkey 之前提到的,AJAX 回调旨在通过加载部分视图保持在同一页面中。如果您想从客户端脚本重定向到另一个页面,则必须使用分配有预期 URL 字符串的location.href,而根本不使用 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()函数将部分视图内容显示到目标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
    });
});

【讨论】:

    猜你喜欢
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    相关资源
    最近更新 更多