我有一个名为JQuery的函数InsertShowStudents()
,该函数在每次按下Submit按钮()时都会正确执行。成功后,我将调用另一个函数GetStudents()
以显示表中元素的更新列表。该GetStudents()
功能仅在第一次按下按钮时调用控制器动作。之后,InsertShowStudents()
继续插入表中,但是该GetStudents()
函数未在控制器上调用Action方法。.我已经使用断点进行了证明,您知道发生了什么吗?
jQuery代码:
$(function () {
$("#btnInsertStudent").click(function () {
InsertShowStudents();
return false;
})
})
function InsertShowStudents() {
$.ajax({
type:"post",
url: "/Students/InsertStudent/",
data: { Name: $("#Name").val(), LastName: $("#LastName").val(), Age: $("#Age").val() }
}).success(function () {
GetStudents();
//clear the form
$("#myform")[0].reset();
}).error(function () {
$("#divGetStudents").html("An error occurred")
})
}
function GetStudents()
{
$.ajax({
type: "get",
url: "/Students/GetStudents"
}).success(function (result) {
$("#divGetStudents").html(result)
})
}
控制器动作:
public ActionResult InsertStudent()
{
return View();
}
[HttpPost]
//[ValidateAntiForgeryToken]
public ActionResult InsertStudent(Student student)
{
if (ModelState.IsValid)
{
db.Students.Add(student);
db.SaveChanges();
return View();
}
return View(student);
}
public PartialViewResult GetStudents()
{
List<Student> students = db.Students.ToList();
return PartialView(students);
}
在第一次工作的情况下,我会假设存在缓存问题。要强制绕过缓存,您可以将选项添加到ajax请求中。有关更多信息,请查看jquery ajax文档。
$.ajax({
type: "get",
url: "/Students/GetStudents",
cache: false
}).success(function (result) {
$("#divGetStudents").html(result)
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句