从控制器获取int值

从控制器获取int值

本文介绍了从控制器获取int值$阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从控制器传递int值并检查它是否大于0的$阿贾克斯。我不知道该怎么做。我已经尝试了code,但它给我未定义的值。在控制器持续的数据是工作的罚款。但回国无法int值的事:

AJAX code:

 函数bindForm(对话框,urlString){
    $('形式',对话).submit(函数(){
    VAR DATA_SEND = $(本).serializeArray();
    $阿贾克斯({
        网址:urlString,
        类型:this.method,
        数据:$(本).serialize()
        成功:函数(结果){
        警报(result.success);
        如果(parseInt函数(result.success,10)大于0){
            警报(信息添加成功');
            $('#对话框的形式')对话框(亲密);
            变种hdnInvoiceId =的document.getElementById(INVOICE_ID);
            hdnInvoiceId.value = parseInt函数(result.success,10);
            $('#addInvoiceDetail')隐藏()。
            }其他{
            警报(请重新输入详细信息');
            bindForm();
            }
        }
    });
    返回false;
});

控制器code:

  [HttpPost]
公众诠释AddInvoice(发票模型)
{
    INT INVOICE_ID = CRMServiceDL.insertInvoiceDetail(模型);
    诠释成功;
    如果(INVOICE_ID大于0)
    {
        成功= INVOICE_ID;
    }
        其他{
        成功= -1;    }
    返回成功;
}


解决方案

您需要返回JSON

  [HttpPost]
公共JsonResult AddInvoice(发票模型)
{
    INT invoiceId = CRMServiceDL.insertInvoiceDetail(模型);
    诠释成功= invoiceId> 0? invoiceId:-1;    返回JSON(新{成功});
}

I want to pass int value from controller and check if it is greater than 0 in $.ajax. 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.:

AJAX Code:

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 });
}

这篇关于从控制器获取int值$阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:49