从Controller获取Int值到$. ajax
本文关键字:ajax 值到 Int Controller 获取 | 更新日期: 2023-09-27 18:06:07
我想从控制器传递int值,并检查它是否大于0在$.ajax。我不知道该怎么做。我试过一个代码,但它给我未定义的值。控制器中的数据工作正常。但是不能返回int值:
AJAX代码:function bindForm(dialog,urlString) {
$('form', dialog).submit(function () {
var data_send = $(this).serializeArray();
$.ajax({
url: urlString,
type: this.method,
data: $(this).serialize(),
success: function (result) {
alert(result.success);
if (parseInt(result.success, 10) > 0) {
alert('Details Added Successfully');
$('#dialog-form').dialog('close');
var hdnInvoiceId = document.getElementById("Invoice_Id");
hdnInvoiceId.value = parseInt(result.success, 10);
$('#addInvoiceDetail').hide();
} else {
alert('Please Re-enter Details');
bindForm();
}
}
});
return false;
});
控制器代码:[HttpPost]
public int AddInvoice(Invoice model)
{
int Invoice_Id = CRMServiceDL.insertInvoiceDetail(model);
int success;
if (Invoice_Id > 0)
{
success = Invoice_Id;
}
else {
success = -1;
}
return success;
}
你需要返回Json
[HttpPost]
public JsonResult AddInvoice(Invoice model)
{
int invoiceId = CRMServiceDL.insertInvoiceDetail(model);
int success = invoiceId > 0 ? invoiceId : -1;
return Json(new { success });
}
在你的成功回调中使用result而不是result.success.
结果本身将包含您返回的值。