0

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.
    });
} 
2
  • 4
    Possible duplicate of How to Call Controller Actions using JQuery in ASP.NET MVC Commented Dec 6, 2018 at 9:26
  • 1
    you can do a .success() and .fail() before you do the done. Just for example do .done((result) => { console.log(result); }); Commented Dec 6, 2018 at 9:28

2 Answers 2

3

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
   });
} 
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.