-1

Below would be the function/method from the controller:

public ModelVoucherLogs getDetails(int id)
{
    VoucherHistoryManager manager1 = new VoucherHistoryManager();
    var m = manager1.FindAll()
        .Where(a => a.ID == id)
        .Single();

    VoucherLogsManager manager = new VoucherLogsManager();

    ModelVoucherLogs model = manager.FindAll()
        .Where(a => a.VoucherNo.Equals(m.VoucherNo) &&
                    a.TransactionType.Equals("CONSUMED"))
        .Single();

    return model;
}

This would be the JavaScript function:

function getDetails(id) {
    var modal = document.getElementById('details_modal');
    modal.style.display = "block";
    $.ajax({
        type: "POST",
        url: "Main/getDetails?id=" + id,
        success: function (model) {

        }
    });
}

How do I get the values of the model individually. Sample: Let's just say the model has Voucher ID, Transaction Type, etc.
How do I get that value of those.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • 4
    `console.log(model)` <-- what does that say? (In your `success` handler) – Cerbrus Jul 10 '18 at 09:26
  • Possible duplicate of [How to send a model in jQuery $.ajax() post request to MVC controller method](https://stackoverflow.com/questions/1518417/how-to-send-a-model-in-jquery-ajax-post-request-to-mvc-controller-method) – Anamnian Jul 11 '18 at 07:11

1 Answers1

-3

Just to make an example, I feel like this should work? Create a variable that we'll for now call returnData, and bind the ajax result (model) into that variable. At the end of the function, return the data, and then we can access it for example in the HandleDetails function. (You might want to make the ajax call synchronous or else a empty variable might get returned)

function getDetails(id) 
    var returnData;
    var modal = document.getElementById('details_modal');
    modal.style.display = "block";
    $.ajax({
        type: "POST",
        url: "Main/getDetails?id=" + id,
        async: false,
        success: function (model) {
            returnData = model;
        }
    });
    
    return returnData;
}

function HandleDetails() {
    var data = getDetails(1);
    
    var voucherId = data.VoucherID;
    var transactionType = data.TransactionType;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Xariez
  • 759
  • 7
  • 24
  • 3
    This answer is not correct. Have you even tried that code? $.ajax makes an ASYNCHRONOUS request, which means that the return of your getDetails happens BEFORE the success function is executed so getDetails will always, always, always return undefined. – A. Llorente Jul 10 '18 at 09:37
  • This answer is broken in multiple ways. This is an synchronous approach to an asynchronous method. That can never work. – Cerbrus Jul 10 '18 at 09:55
  • @A.Llorente I'm thinking you didn't read the last part of my post, then? Clearly stating exactly what you just did, ie. if the AJAX call is asynchronous (which it currently is), it wil return a undefined/null variable before the request has a chance to do it's thing? (Though I appreciate feedback and always will, whether I am the one trying to provide a solution or asking for one) – Xariez Jul 10 '18 at 10:33
  • 1
    Well, you just edited your code to make it synchronous and if I remember correctly there was no mention to synchronity in your answer before editing, when I posted my comment it was 100% valid and accurate. Anyway, making an AJAX call synchronous is bad practice, you would better make the function async or return a promise that resolves in the success function. – A. Llorente Jul 10 '18 at 11:01
  • @A.Llorente I did edit it, however only the code part. "(You might want to make the ajax call synchronous or else a empty variable might get returned)" was there from the very start. But again, I don't want to start a fight with someone, I, for one, value feedback greatly. – Xariez Jul 10 '18 at 12:51
  • Yes, dude no hard feelings whatsoever ;) We are here to learn and to share! – A. Llorente Jul 10 '18 at 12:56