Ajax for retrieving data and changing elements based on data retrieved

This is more of an informational question, and perhaps the question is not well formatted, but I wouldn't know how to explain it any better.

I have made a controller for retrieving data from the database [HttpGet], how can I use Ajax for the retrieval of the a fore mentioned data, and if successful change the color of the div?

  [HttpGet]
    public List<AppointmentsDT> ReturnApps()
    {
        List<AppointmentsDT> list = new List<AppointmentsDT>();

        foreach (Appointment a in db.Appointments)
        {
            AppointmentsDT a1 = new AppointmentsDT();
            a1.AppWithCompanyName = a.AppWithCompanyName;
            a1.AppTime = a.AppTime;

            list.Add(a1);
        }
        return list;
    }

I am drawing a blank as to how to write a proper syntax of ajax that would change the color of the appropriate div based on that data it received, for example if in the database there is a company named C and time named T then div1 would change color?

Is it even possible? Is there a code sample I could use? Like I said I'm drawing a blank since I'm still learning Ajax...any advice would be helpful. TNX

Answers 1

  • I would suggest you to use AJAX to call an ASP.NET Web API and call your controller from the API function.

    Your AJAX function can bee something like this

    $.ajax({
          contentType: "application/json; charset=utf-8",
          type: 'GET',
          url:'api/getValues', //Your API url
          dataType: 'json',
          success: function (result) {
                 //Use your logic to change color code here
                 },
          error: function (error) {
                 alert("error");
                 }
         });
    

    The result inside the success function will have your desired list and you could check conditions there to manipulate your div


Related Articles