【发布时间】:2018-09-13 03:47:46
【问题描述】:
当单击按钮时,我使用 AJAX 从 JavaScript 调用了 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