Как вставить несколько записей в базу данных одновременно ajax в ASP. NET Core MVC с Entity Framework - PullRequest
0 голосов
/ 23 марта 2020

Я хочу сохранить записи в двух моделях, где одна модель может получить одну строку, а вторая модель может получить несколько строк. Пожалуйста, помогите мне решить эту проблему.

Контроллер: PatientTestsController.cs

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> AddPatientTest(PatientTestsAndBilling ptb)
{
        if (ModelState.IsValid)
        {
        }

        return View(ptb);
}

Модель: PatientTest

public class PatientTest
{
    [Key]
    public int Id { get; set; }

    [Display(Name ="Patient Id")]
    public int PatientId { get; set; }

    [Display(Name ="Test Id")]
    public int TestId { get; set; }

    [Display(Name ="Doctor")]
    public int DoctorId { get; set; }

    [Display(Name="Center")]
    public int CenterId { get; set; }

    [Display(Name = "Test")]
    [ForeignKey("TestId")]
    //relation of Tests table
    public virtual Tests Tests { get; set; }

    [Display(Name = "Doctor Reference")]
    [ForeignKey("DoctorId")]
    //relation of Doctors table
    public virtual Doctors Doctors { get; set; }

    [Display(Name = "Center Reference")]
    [ForeignKey("CenterId")]
    //relation of Centers table
    public virtual Centers Centers { get; set; }

    [Display(Name = "Patient")]
    [ForeignKey("PatientId")]
    //relation of Patient table
    public virtual Patient Patient { get; set; }
}

Модель: PatientBilling

public class PatientTest
{
    [Key]
    public int Id { get; set; }

    [Display(Name ="Patient Id")]
    public int PatientId { get; set; }

    [Display(Name ="Test Id")]
    public int TestId { get; set; }

    [Display(Name ="Doctor")]
    public int DoctorId { get; set; }

    [Display(Name="Center")]
    public int CenterId { get; set; }

    [Display(Name = "Test")]
    [ForeignKey("TestId")]
    //relation of Tests table
    public virtual Tests Tests { get; set; }

    [Display(Name = "Doctor Reference")]
    [ForeignKey("DoctorId")]
    //relation of Doctors table
    public virtual Doctors Doctors { get; set; }

    [Display(Name = "Center Reference")]
    [ForeignKey("CenterId")]
    //relation of Centers table
    public virtual Centers Centers { get; set; }

    [Display(Name = "Patient")]
    [ForeignKey("PatientId")]
    //relation of Patient table
    public virtual Patient Patient { get; set; }
}

Модель: PatientTestAndBilling

public class PatientTestsAndBilling
{
    public List<PatientTest> patientTests { get; set; }
    public PatientBilling patientBilling { get; set; }
}

Это мой jQuery код ..

 $("#save").click(function () {
        var row = $('#table2 tbody tr').length;
        var patientTest=[];
        var patientBilling;
        var j;
        for (var i = 0; i < row; i++) {
            j = i + 1;
            patientTest.push({
                PatientId: $('#patientId').val(),
                TestId: $('#table2 tbody tr:nth-child('+j+')').attr('id'),
                DoctorId: $('#doctor').val(),
                CenterId: $('#center').val(),

            });
        }
        patientBilling= {
                PatientId: $('#patientId').val(),
                TotalPrice: $('#total_price').text(),
                PaidAmt: $('#total_paid').text(),
                BalanceAmt: $('#balance').text(),
                OtherCharges: $('#ot_charges').val(),
                Discount: $('#discount').val(),
                PaymentType: $('#payType').val()
        };
        var model = {
            "patientTest": patientTest,
            "patientBilling": patientBilling
        }
        alert(JSON.stringify(model));
        $.ajax({
            type: "POST",
            url: "/Reception/PatientTests/AddPatientTest",
            data: model,
            headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
            success: function (data) {
                alert(data);
            }
        });
    });

В этом примере я получаю только данные о счете пациента, а не данные теста пациента.

Пожалуйста, помогите мне решить эту проблему ..

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...