我在单击按钮时使用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((内,我返回了视图,它应该加载另一个视图。但是我只会在警报中获得"成功",但没有加载新的视图。我认为,应加载视图。
您正在对服务器进行" ajax"调用,这意味着您的请求在当前页面请求之外运行,结果将返回到您的success
持续例程,而不是浏览器的渲染引擎。本质上,该例程的data
参数可能是您UploadFile
视图的整个HTML响应。
这不是.ajax
的目的。它是为了向服务器提出异步请求,并将数据(通常是JSON或XML(返回到您的JavaScript中以评估并显示在页面上(无论如何这是最常见的用途(。
我看不到您的HTML,但是只要使用<a>
锚(链接(标签并将您的学生ID发送在查询字符串上,您会不会更好?很难说您尝试做什么,但是您的视图'html(.cshtml(永远不会使用您现在拥有的代码显示。
看来,您没有加载视图返回到ajax成功。在下面的代码中,将# divname
替换为代码中的div。我希望这有助于解决您的问题
<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.
}