2016-07-08 4 views
2

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); 
} 
+0

あなたはキャッシュを持っていますかチェックしてください! – curtainrising

+0

使用しているjQueryのバージョン。 –

+0

@NiketanRaval jquery-1.10.2.js – AlexGH

答えて

1

をお試しください問題。キャッシュを強制的にバイパスするには、オプションをajaxリクエストに追加できます。詳細についてはjquery ajax documentation

$.ajax({ 
 
     type: "get", 
 
     url: "/Students/GetStudents", 
 
     cache: false 
 
    }).success(function (result) { 
 
     $("#divGetStudents").html(result) 
 
    })

0

初めて私はキャッシュを想定したい取り組んでいるところ、このような状況では、この

function InsertShowStudents() { 
    $.ajax({   
     type:"post", 
     url: "/Students/InsertStudent/", 
     data: { Name: $("#Name").val(), LastName: $("#LastName").val(), Age: $("#Age").val() } 
    }).done(function() {   
     GetStudents(); 
     //clear the form 
     $("#myform")[0].reset(); 
    }).fail(function() { 
     $("#divGetStudents").html("An error occurred") 
    }) 
} 

function GetStudents() 
{ 
    $.ajax({ 
     type: "get", 
     url: "/Students/GetStudents" 
    }).done(function (result) { 
     $("#divGetStudents").html(result) 
    }) 
} 
+0

が働いていなかったので、私は解決策を提示しました。問題がActionメソッドにあったようです – AlexGH

関連する問題