我需要从控制器发送一条错误消息,以防 ajax 函数发生错误。这是我的控制器代码:
//check if the day is already approved
string check1 = "SELECT approve_status_id FROM table_approvals WHERE approve_date=" + date + " AND user_id=" + Session["userID"];
MySqlCommand status_cmd = new MySqlCommand(check1, con);
MySqlDataReader rdrStatus = status_cmd.ExecuteReader();
while (rdrStatus.Read())
{
if (rdrStatus.GetString(0) == "5")
{
valid = false;
error = "You cannot edit already approved dates!";
response.Message = error;
return View(Json(new { success = false,error},JsonRequestBehavior.AllowGet));
}
}
我的ajax函数:
$.ajax({
url: '@Url.Action("SaveLine", "AddLine")',
type: 'post',
data: { ids: IDs },
dataType: 'json',
traditional: true,
success: function () {
window.location.href = '@Url.Action("Index", "AddLine")';
},
error: function () {
$.getJSON('/AddLine/SaveLine', function (json) {
alert('error');
$("#ajaxPostMessage").html(json.Message);
});