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

411 Views Asked by At

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.

1

There are 1 best solutions below

0
Fei Han On BEST ANSWER

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);
}