下面是相關查看CSHTML代碼爲什麼標記爲HttpPost的MVC控制器操作方法沒有從外部JavaScript文件調用?
<tbody>
@foreach (var task in Model.TasksSummaryList)
{
<tr>
<th onclick="OnPatientSelected('@task.PatientID')">@task.PatientName</th>
<th>@task.Status</th>
<th>@task.TaskCount</th>
<th>@task.MostRecentTask</th>
</tr>
}
</tbody>
這裏的JavaScript文件,其中包含OnPatientSelected外部
function OnPatientSelected(selectedPatientID) {
$.ajax({
type: "POST",
url: "TasksSummary/TaksSummaryDetailsView",
data: { selectedPatientID: selectedPatientID },
error: function() {
alert("fail");
},
success: function(){
alert("success");
}
});
}
這始終顯示成功,但永遠不會調用控制器ActionMethod我想
[AllowAnonymous]
[HttpPost]
public ActionResult TasksSummaryDetailsView(/*data from view*/string selectedPatientID)
{
int i = 09;
//Received PatientID from View (Client)
//Browse to the appropriate view
return View();
}
你可以保持一個斷點在您操作方法,並檢查它是否被擊中 –
您是如何驗證該操作未被調用的?另外,請注意,提供的'url'是[相對於當前頁面的地址](https://stackoverflow.com/questions/4765740/relative-urls-in-ajax-requests),並且可能不會導致您期望的路徑。 –
嘗試使用fiddler來查看正在進行的呼叫以及輸出是什麼。另外,直接在瀏覽器中試試看看它是否有效:'.../TasksSummary/TaksSummaryDetailsView?selectedPatientID =「yourvalue」' – TheUknown