【问题标题】:View not loaded when ActionResult is called from AJAX in ASP.NET MVC从 ASP.NET MVC 中的 AJAX 调用 ActionResult 时未加载视图
【发布时间】:2018-09-13 03:47:46
【问题描述】:

当单击按钮时,我使用 AJAXJavaScript 调用了 actionresult 函数:

 <script type="text/javascript">
        $('.btn-SelectStudent').on('click', function () {
            $('input:radio').each(function () {
                if ($(this).is(':checked')) {
                    var id = $(this).val();
                    $.ajax({
                        url: '@Url.Action("ParseSearchLists", "Upload")',
                        data: { studentId: id }
                    }).success(function (data) {
                       alert('success');
                    });
                }
                else {
                    // Or an unchecked one here...
                }
            });

            return false;
        })
    </script>

UploadController.cs 内部:

    [HttpPost]
    public ActionResult ParseSearchLists(int studentId)
    {
        SearchModel searchModel = ApplicationVariables.searchResults.Where(x => x.StudentId == studentId).ToList().First();
        TempData["SearchModel"] = searchModel;
        return RedirectToAction("UploadFile", "Upload");
    }

    public ActionResult UploadFile()
    {
        searchModel = TempData["SearchModel"] as SearchModel;
        return View(searchModel); //debug point hits here.
    }

在 UploadFile() 中,我返回了 View,它应该加载另一个视图。但是我在警报中只得到“成功”,但没有加载新视图。我认为,应该加载视图。

【问题讨论】:

  • 一些事情。 1) 您不能在 AJAX 调用中返回 RedirectToAction,处理程序不知道如何处理它。 2)在您的 AJAX 调用的 success 函数中,您没有对从服务器收到的数据做任何事情,您只是在提醒成功
  • 那么,在这种情况下,最好的选择是什么。我在模型类中有一些数据,需要将其传递到另一个页面并呈现它。
  • 我认为 ajax 成功,我只得到纯 html 或一些简单的字符串,而不是 Model 类。
  • 当您从 ajax 调用时,不会调用 RedirectToAction,最好返回一个带有您要重定向的 URL 的 json,并且在 ajax 请求中您可以使用 window.location = "localhost:3000/Upload/UploadFile";

标签: asp.net asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

您正在对服务器进行“AJAX”调用,这意味着您的请求在当前页面请求之外运行,结果将返回到您的success 延续例程,而不是浏览器的呈现引擎。本质上,该例程的 data 参数可能是您的 UploadFile 视图的整个 HTML 响应。

这不是.ajax 的用途。它用于向服务器发出异步请求并将数据(通常是 JSON 或 XML)返回到您的 javascript 以进行评估并显示在页面上(无论如何这是最常见的用途)。

我看不到您的 HTML,但是您最好使用 &lt;a&gt; 锚(链接)标签并在查询字符串中发送您的学生 ID 不是更好吗?很难说您正在尝试做什么,但您的视图的 HTML (.cshtml) 将永远不会使用您现在拥有的代码显示。

【讨论】:

    【解决方案2】:

    看来,您没有在 ajax 成功时加载视图返回到 div。在下面的代码中用您的 div 替换 #divname。我希望这有助于解决您的问题

     <script type="text/javascript">
                $('.btn-SelectStudent').on('click', function () {
                    $('input:radio').each(function () {
                        if ($(this).is(':checked')) {
                            var id = $(this).val();
                            $.ajax({
                                url: '@Url.Action("ParseSearchLists", "Upload")',
                                dataType: "html",
                                data: { studentId: id }
                            }).success(function (data) {
                              $('#divName').html(data); // note replace divname with your actual divname
                            });
                        }
                        else {
                            // Or an unchecked one here...
                        }
                    });
    
                    return false;
                })
            </script>
          [HttpPost]
            public ActionResult ParseSearchLists(int studentId)
            {
                SearchModel searchModel = ApplicationVariables.searchResults.Where(x => x.StudentId == studentId).ToList().First();                
                return View("UploadFile",searchModel); //debug point hits here.
            }
    

    【讨论】:

    • 我想用模型类重定向到另一个视图。
    • 包括 ViewName 作为回报。像 return View("ViewName", searchModel);我用您的视图名称更新了答案。我想你想导航到那个“上传文件”View.hope that heps!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 2020-09-02
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多