[英]Get Int value from Controller to $.Ajax
I want to pass int value from controller and check if it is greater than 0 in $.ajax. 我想从控制器传递int值,并检查它在$ .ajax中是否大于0。 I don't know how to do it.
我不知道该怎么做。 I have tried a code but it is giving me undefined value.
我已经尝试过一个代码,但是它给了我未定义的值。 Going data in controller is working fine.
控制器中的数据正常运行。 But returning int value not able to do.:
但是返回int值不行。
AJAX Code: 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;
});
Controller Code: 控制器代码:
[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;
}
You need to return Json 您需要返回杰森
[HttpPost]
public JsonResult AddInvoice(Invoice model)
{
int invoiceId = CRMServiceDL.insertInvoiceDetail(model);
int success = invoiceId > 0 ? invoiceId : -1;
return Json(new { success });
}
Just use result in your success callback rather then result.success. 只需在您的成功回调中使用result,然后再使用result.success即可。
Result itself will contain the value you returned. 结果本身将包含您返回的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.