ajax in asp.net core entity framework core 发布数据后如何显示更新内容

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

我想在 jquery ajax 发布数据后显示更新内容。我试过了,但不显示数据。 这是我的代码...

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) {

            }
        });
    });        
})

控制器

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

帮我解决这个问题。

根据您的代码,您向操作方法发出 ajax 对 PatientBilling 的 post 数据的请求。要在请求成功完成后在Details页面上显示新增的PatientBilling信息,可以在成功回调函数中做重定向,如下所示。

<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>

详情操作

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