Search code examples
ajaxasp.net-coreasp.net-ajaxasp.net-core-2.0jquery-ajaxq

How to display update content after posting data by ajax in asp.net core entity framework core


I want to display update content after posting data by jquery ajax. I tried it but not display data. here is my code...

jquery ajax

 $(document).ready(function () {

    $('#paid').click(function () {
        var pid = $('#pid').val();
        var amt = $('#amt').val();
        var payType = $('#payType').val();
        $.ajax({
            type: "POST",
            url: "/Reception/Registration/AddPaidBalance",
            data: { PatientId: pid, PaidAmt: amt, PaymentType: payType },
            success: function (data) {

            }
        });
    });        
})

controller

 [HttpPost]
    public async Task<IActionResult> AddPaidBalance(PatientBilling patientBilling)
    {
        if (ModelState.IsValid)
        {
            _db.PatientBilling.Add(patientBilling);
            await _db.SaveChangesAsync();
            //return RedirectToAction("Details", "Registration", new { area = "Reception", id = patientBilling.PatientId });
            //return RedirectToAction("Details", "Registration", new { patientBilling.PatientId});
        }

        return View();
    }

help me out from this issue.


Solution

  • Based on your code, you make ajax request to post data of PatientBilling to action method. To display new-added PatientBilling information on Details page after the request completes successfully, you can do redirection in success callback function, like below.

    <script>
        $(document).ready(function () {
    
            $('#paid').click(function () {
                var pid = 1;//$('#pid').val();
                var amt = "amt";//$('#amt').val();
                var payType = "type1";//$('#payType').val();
                $.ajax({
                    type: "POST",
                    url: "/Reception/Registration/AddPaidBalance",
                    data: { PatientId: pid, PaidAmt: amt, PaymentType: payType },
                    success: function (data) {
                        window.location.href = "@Url.Action("Details", "Registration", new { Area = "Reception"})" + "?patientId=" + pid;
                    }
                });
            });
        })
    </script>
    

    Details action

    public IActionResult Details(int patientId)
    {
        // code logic here
        // get details of PatientBilling based on received patientId
        // ...
        return View(model);
    }