Home > database >  MVC Java script loop async controller data
MVC Java script loop async controller data

Time:10-20

I'm running a script and want to output an alert when it is finished loading all data. The problem is, I have a for loop and call a MVC controller function, but the alert is outputted/triggered bevor the data is loaded completely. It seems like the data fetching from the controller runs asynchronously.

I added the async and await but it changed nothing.

Script:

async  function checkform() {
        $("#faRueckmeldungen").html("")
        let _clipboard="";
        var _fanr = $('#FaNr').val();     
        
        var _faArray = _fanr.replace(/\n/g, " ").split(" ");

      for (var i = 0; i <= _faArray.length; i  ) {

            var _fA = _faArray[i];

            if (_fA < 2013810000) {
                (document.getElementById("FaNr")).style.backgroundColor = "OrangeRed";
                alert("FaNr #"   _fA   " nicht korrekt!");
            }
            else {

                var url = "/FaInfos/FaRueckmeldung";
                $.get(url, { faNr: _fA }, await function (data) {                  

                    $("#faRueckmeldungen").append(data[0]);
                    _clipboard  = data[1];
                     navigator.clipboard.writeText(_clipboard);    
                })
          }
     } 
     alert("finished")
    }

Controller function:

    public  JsonResult FaRueckmeldung(string faNr)
    {
        if (faNr == null)
            return null;

        //Loading data from SAP
        var faHead = SapInterClient.GetFaHead(faNr.ToString());
        var faPos = SapInterClient.GetFaPos(faNr.ToString());
        //<td>"  "</td>

        string clippoard = "Fertigungsauftrag\tMaterialnummer\tAuftragsmenge\tStart Termin Soll\tStart Termin Ist\tEndtermin Soll\tEndtermin Ist\tPersonal Soll [min]\tPersonal Ist [min]";

        string sret = "<table table1"   (char)34   @"><thead><tr>
        <th>Fertigungsauftrag</th>
        <th>Materialnummer</th>
        <th>Auftragsmenge</th>
        <th>Start Termin Soll</th>
        <th>Start Termin Ist</th>
        <th>Endtermin Soll</th>
        <th>Endtermin Ist</th>
        <th>Personal Soll [h]</th>
        <th>Personal Ist [h]</th>";

        decimal VgPers = 0;
        decimal RuPers = 0;

        for (int i = 0; i < faPos.RückgemeldeteLeistungRP.Count(); i  )
        {
            VgPers  = faPos.RückgemeldeteLeistungTP[i];//VorgabePersonal
            RuPers  = faPos.RückgemeldeteLeistungRP[i];//FeedbackPersonal
        }

        string cellColor = "LightGreen";

        if (VgPers/60 < RuPers)
            cellColor = "Salmon";


        foreach (var item in faPos.Vorgangsnummer)
        {
            clippoard  = "\t VG "   item;
            sret  = "<th> VG "   item   "</th>";
        }
        clippoard  = "\r\n";
        sret  = "</tr></thead>";

        sret  = "<tr><td>"   faNr   "</td><td>"   faHead.MatNr   "</td><td>"   faHead.GesamteAuftragsmenge   "</td><td>"   faHead.TerminierterStart.ToString("dd.MM.yyyy")   "</td><td>"   faHead.IstStartTermin.ToString("dd.MM.yyyy")   "</td><td>"   faHead.TerminiertesEnde.ToString("dd.MM.yyyy")   "</td><td>"   faHead.IstEndTermin.ToString("dd.MM.yyyy")   "</td><td>"   String.Format("{0:0.00}", VgPers / 60)   "</td><td bgcolor="   (char)34   cellColor   (char)34   ">"   String.Format("{0:0.00}", RuPers)   "</td>";
        clippoard  = faNr   "\t"   faHead.MatNr   "\t"   faHead.GesamteAuftragsmenge   "\t"   faHead.TerminierterStart.ToString("dd.MM.yyyy")   "\t"   faHead.IstStartTermin.ToString("dd.MM.yyyy")   "\t"   faHead.TerminiertesEnde.ToString("dd.MM.yyyy")   "\t"   faHead.IstEndTermin.ToString("dd.MM.yyyy")   "\t"   String.Format("{0:0.00}", VgPers / 60)   "\t"   String.Format("{0:0.00}", RuPers);

        for (int i = 0; i < faPos.GutmengeGesamt.Count(); i  )
        {
            clippoard  = "\t"   faPos.GutmengeGesamt[i];
            sret  = "<td>"   faPos.GutmengeGesamt[i]   "</td>";
        }
        clippoard  = "\r\n";
        sret  = "</tr></table>";

        return  Json(new List<string>{sret,clippoard}, JsonRequestBehavior.AllowGet);
    }

Please help.

Best regards Simon

CodePudding user response:

You forgot to add await before the $.get(url....). The await before function has no reason to be there. Add async if you make asynchronous calls in there, though I do not see any.

The idea is that you add await to the specific instruction that worka asynchronous ( and you want to wait for it). In your case, your callback function that you send to get is not the async function, rather the .get() itself is the guy you're looking for. The callback function you send there is being executed after the api call is done, and it's handled by .get() itself.

So your code will look a little something like this:

await $.get(
        url,
        { faNr: _fA },
        function (data) {                  
           $("#faRueckmeldungen").append(data[0]);
           _clipboard  = data[1];
           navigator.clipboard.writeText(_clipboard);    
        }
      )
  • Related