如何在HttpPost方法中将第一个表的id保存在第二个表的id中

时间:2018-02-19 10:07:53

标签: c# .net asp.net-mvc

我现在有2个表我希望在患者表的Center_ID中保存CentreId,同时在ASP.NET MVC的HttpPost方法中添加患者数据。如何自动获取并保存?

这是一段代码。

我在病人表中提到了centerid的会话ID。现在我想将它保存在患者表列中,但它是null。请提及正确的方法。

public ActionResult patientRegistration(AllViewModel patient)
{
    if (ModelState.IsValid)
    {
        try
        {

            int Id = (int)Session["ID"];

            var data = new PatientRegistration
            {
                CentreId = Id,
                Patient_Name = patient.Prefix + " " + patient.Patient_Name,
                Patient_DOB = patient.Patient_DOB,
                Gender = patient.Gender,
                TwoDDiagnostic_name = patient.TwoDDiagnostic_name,
                TwoDDiagnostic_price = patient.TwoDDiagnostic_price,
                ThreeDDiagnostic_name= patient.ThreeDDiagnostic_name,
                ThreeDDiagnostic_price = patient.ThreeDDiagnostic_price,
                RefferalDoctor = patient.RefferalDoctor,
                Patient_Reports = patient.Patient_Reports,
                Patient_MobileNo = patient.Patient_MobileNo,
                Patient_Email = patient.Patient_Email,
                Patient_TelephoneNo = patient.Patient_TelephoneNo,
                Patient_State = patient.Patient_State,
                Patient_City = patient.Patient_City,
                Patient_Address = patient.Patient_Address,
                Patient_Area = patient.Patient_Area,
                Patient_PaymentMode = patient.Patient_PaymentMode,
                Patient_PaymentAmount = patient.Patient_PaymentAmount,
                Patient_Date = DateTime.Now.Date,
                BankName = patient.BankName,
                chequedate=patient.chequedate,
                ChequeNo = patient.ChequeNo,
            };
            db.PatientRegistrations.Add(data);
            db.SaveChanges();
            State_Bind();
            City_Bind();

            return RedirectToAction("PatientDetails");
            //ViewData["CentreCode"] = Session["CenterCode"];
        }
        catch (DbEntityValidationException e)
        {
            foreach (var eve in e.EntityValidationErrors)
            {
                Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                    eve.Entry.Entity.GetType().Name, eve.Entry.State);
                foreach (var ve in eve.ValidationErrors)
                {
                    Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                        ve.PropertyName, ve.ErrorMessage);
                }
            }
            throw;
        }

    }
    try
    {

    }
    catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
    {
        Exception raise = dbEx;
        foreach (var validationErrors in dbEx.EntityValidationErrors)
        {
            foreach (var validationError in validationErrors.ValidationErrors)
            {
                string message = string.Format("{0}:{1}",
                    validationErrors.Entry.Entity.ToString(),
                    validationError.ErrorMessage);
                // raise a new exception nesting  
                // the current instance as InnerException  
                raise = new InvalidOperationException(message, raise);
            }
        }
        throw raise;
    }
    State_Bind();
    return View(patient);

}

0 个答案:

没有答案