簡體   English   中英

使用JQuery AJAX從Controller獲取價值

[英]Get value from Controller using JQuery AJAX

我想從controller獲取新值並將其設置為在提交表單時view

控制器:

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

我的Jquery函數:

我只想在調用此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.
    });
} 

數據應該在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
   });
} 

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