简体   繁体   中英

Get value from Controller using JQuery AJAX

I want to get new value from controller and set it to view when form submit.

Controller:

public JsonResult GetVoucherNo()
    {
        var voucherno = 1001;

        var lastvoucherno = db.PaymentsAndReceipts.Where(x => x.StakeHolder.StakeHolderType.Name == "Supplier").OrderBy(x => x.VoucherNo).ToList().LastOrDefault();

        if (lastvoucherno != null)
        {
            voucherno = lastvoucherno.VoucherNo.GetValueOrDefault() + 1;
        }

        return new JsonResult { Data = voucherno, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    }

My Jquery function:

I just want to do when this function calls, I can get the value from controller action .

function getVoucherNo()
{
    $.ajax({
        contentType: 'application/json; charset=utf-8',
        url: 'Payments/GetVoucherNo',
        dataType: "json"
    }).done(function () {
    //don't know what to do here.
    });
} 

The data should be available in the done function argument like this:

function getVoucherNo()
{
   $.ajax({
      contentType: 'application/json; charset=utf-8',
      url: 'Payments/GetVoucherNo',
      dataType: "json"
   }).done(function (result) {
     //don't know what to do here.
     console.log(result);
     //you should see the exact JSON you return from the controller
   });
} 

I done it with success .

function getVoucherNo()
{
    $.ajax({
        contentType: 'application/json; charset=utf-8',
        url: '/Payments/GetVoucherNo',
        dataType: "json",
        success: function (result) {
            console.log(result);
            return $('#VoucherNo').val(result); //to set value in textbox.
        },
        error: function (xhr, status, error) {
            alert(xhr);
            console.log(xhr, status, error);
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM