Q:

Asp.net Core 3.1, using Ajax to display json data on view

I wanna load data from the GetPatient Function the labels on the view for create.  this is what i have so far, it is getting the data by id, but not returing it as json or showing on said labels.  How do i fix this? 

 // GET: Appointments/Create
        public IActionResult Create(int id = 0)
        {
            if (id == 0)
                return View(new Appointments());
            else
                return View(_context. Appointments. Find(id));

        }

        [HttpGet]
        public JsonResult GetPatient(int id = 0)
        {
            var Pt = _context. Mstr_Patients. Find(id);
            return Json(Pt);
        }
-------- Create View------------
<script>
            function GetPatient() {
            var url = "@Url. Action("GetPatient")";
            var id = $("#PID").val();
            $.get(url, { id: id }, function (data) {
                $("#PID").html(data. PatientID);
            });
        }
        $("#btnSearch").on("click", GetPatient);
</script>

A:

Maybe you could try this:
[HttpGet]
public IActionResult GetPatient(int id = 0)
{
     if (id == 0)
     {
         return View(new Appointments());
     } 
     else
     {
         var patient = _context. Mstr_Patients. Find(id);

         if(patient == null)
         { 
             return StatusCode(404);
         } 

         var serialized = JsonConvert. SerializeObject(patient);

         //This will make your content-type as JSON. 
         return Content(serialized, "application/json");
      }   
}

And in your View:
<script>

    function GetPatient(){
       var PID = document.getElementById("#PID");  
       var id = PID.val();
       $.ajax({
            type: "GET",            
            url: "@Url. Action("GetPatient")";
            contentType: "application/json",
            dataType: "json",
            data: {"id": id}
            success: function (data) {
                $("#PID").html(data. PatientID);
            }
     }

     var btnSearch = document.getElementById("#btnSearch");

     btnSearch.onclick = () => {
            GetPatient();
     }

</script>

Note: I'm not 100% sure but in your AJAX call, you might need to change:
data: {"id": id} to data: {id: id} 

