我正在尝试使用 ajax 进行一些验证。它从我的 MVC3 控制器调用一个方法,但无论控制器返回 true 还是 false,它都返回 true。始终显示警报。
ClientChoices/HasJobInProgress
public Boolean HasJobInProgress(int clientId)
{
return false;
//return _proxy.GetJobInProgress(clientId);
}
jQuery.Ajax
$("#saveButton").click(function() {
var hasCurrentJob =
$.ajax({
url: '@Url.Action("HasJobInProgress", "ClientChoices")/',
data: { id: @Model.ClientId },
success: function(data){
return data;
}
});
if (hasCurrentJob) {
alert("The current clients has a job in progress. No changes can be saved until current job completes");
}
});
解决方案
$("#saveButton").click(function() {
$.ajax({
url: '@Url.Action("HasJobInProgress", "ClientChoices")/',
data: { id: '@Model.ClientId' },
success: function(data){
showMsg(data);
},
cache: false
});
});
function showMsg(hasCurrentJob) {
if (hasCurrentJob.toString()=="True") {
alert("The current clients has a job in progress. No changes can be saved until current job completes");
}
}