InsertShowStudents()
というJQuery関数は、送信ボタン()が押されるたびに正しく実行されます。成功すると、別の関数GetStudents()
を呼び出して、テーブルの要素の更新されたリストを表示します。 GetStudents()
関数は、最初にボタンが押されたときにコントローラアクションを呼び出すだけです。その後、InsertShowStudents()
はテーブルに挿入され続けますが、GetStudents()
関数はコントローラ上のアクションメソッドを呼び出さない。私はブレークポイントを使ってそれを証明した。何が起きているのか知っている?JQuery関数は、最初の送信ボタンが押されたときにコントローラーアクションを呼び出すだけです。
の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);
}
あなたはキャッシュを持っていますかチェックしてください! – curtainrising
使用しているjQueryのバージョン。 –
@NiketanRaval jquery-1.10.2.js – AlexGH