简体   繁体   English

使用JQuery AJAX从Controller获取价值

[英]Get value from Controller using JQuery AJAX

I want to get new value from controller and set it to view when form submit. 我想从controller获取新值并将其设置为在提交表单时view

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: 我的Jquery函数:

I just want to do when this function calls, I can get the value from controller action . 我只想在调用此function时执行操作,就可以从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: 数据应该在done函数参数中可用,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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