使用实体从ASP中的两个不同表转换为View

时间:2017-07-02 00:53:40

标签: c# asp.net entity-framework

我现在遇到一个大问题,

我需要将 dataview 信息转发到" 修改视图"但问题是,数据视图是由两个差异表组成的字段。

现在的主要观点是,如何将两个不同的表转换为我的 PatientView 的一个对象,你能帮帮我吗?

这是编辑控制器

public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var patient = await _db.Patients.FindAsync(id);

            if (patient == null)
            {
                return HttpNotFound();
            }

            var person = await _db.People.FindAsync(patient.PersonId);

            if (person == null)
            {
                return HttpNotFound();
            }     

         //in that place is that i need to send a PatientView
        return View(person);
    }

如果您需要,这是我的DataView

[NotMapped]
    public class PatientView: Person 
    {
        public int Record { get; set; }
        public int PatientId { get; set; }
        [Display(Name = "Fecha de Ingreso")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime? CreationDate { get; set; }

        [Display(Name = "Seguro Principal")]
        public int InsuranceId { get; set; }
        [Display(Name = "Tipificacion Sanguinea")]
        public int BloodTypeId { get; set; }

        [Display(Name = "Edad")]
        [MaxLength(10, ErrorMessage = "La longitud maxima del campo es {1} caracteres")]
        public string Age { get; set; }

        [Display(Name = "Imagen")]
        public HttpPostedFileBase ImageFile { get; set; }  

    }


   This is my Person Table



public class Person
    {
        [Key]
        public int PersonId { get; set; }

        //[Required(ErrorMessage = "El Campo es requerido")]
        [MaxLength(15, ErrorMessage = "La longitud maxima del campo es {1} caracteres")]
        [Display(Name = "RNC/Cedula")]
        public string Rnc { get; set; }

        [Required(ErrorMessage = "El Campo es requerido")]
        [MaxLength(50, ErrorMessage = "La longitud maxima del campo es {1} caracteres")]
        [Display(Name = "Nombre")]
        public string Name { get; set; }

        [Required(ErrorMessage = "El Campo es requerido")]
        [MaxLength(50, ErrorMessage = "La longitud maxima del campo es {1} caracteres")]
        [Display(Name = "Apellido")]
        public string LastName { get; set; }

        [Display(Name = "Fecha de Nacimiento")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime? BornDate { get; set; }

        [Display(Name = "Genero")]
        public int GenderId { get; set; }
        [Display(Name = "Nivel Escolar")]
        public int? SchoolLevelId { get; set; }

        [Display(Name = "Nacionalidad")]
        public int CountryId { get; set; }

        [MaxLength(50, ErrorMessage = "La longitud maxima del campo es {1} caracteres")]
        [DataType(DataType.EmailAddress)]
       // [Index("Person_Email_Index", IsUnique = true)]
        [Display(Name = "Correo")]
        public string Email { get; set; }

        [MaxLength(50, ErrorMessage = "La longitud maxima del campo es {1} caracteres")]
        [DataType(DataType.PhoneNumber)]
       // [Index("Person_Tel_Index", IsUnique = true)]
        [Display(Name = "Telefono")]
        public string Tel { get; set; }

        [MaxLength(50, ErrorMessage = "La longitud maxima del campo es {1} caracteres")]
        [DataType(DataType.PhoneNumber)]
    //    [Index("Person_Cel_Index", IsUnique = true)]
        [Display(Name = "Celular")]
        public string Cel { get; set; }

        [Display(Name = "Estatus Marital")]
        public int MaritalSituationId { get; set; }

        [Display(Name = "Ocupacion")]
        public int OcupationId { get; set; }

        [Display(Name = "Religion")]
        public int ReligionId { get; set; }

       // [Required(ErrorMessage = "El Campo es requerido")]
        [MaxLength(200, ErrorMessage = "La longitud maxima del campo es {1} caracteres")]
        [DataType(DataType.MultilineText)]
        [Display(Name = "Direccion")]
        public string Address { get; set; }

        [Display(Name = "Estatus")]
        public int StatusId { get; set; }

        [DataType(DataType.ImageUrl)]
        [Display(Name = "Imagen")]
        public string Imagen { get; set; }

        [Display(Name = "Autor")]
        public int AuthorId { get; set; }

        public virtual Gender Gender { get; set; }
        public virtual MaritalSituation MaritalSituation { get; set; }
        public virtual Ocupation Ocupation { get; set; }
        public virtual Religion Religion { get; set; }
        public virtual Country Country { get; set; }
        public virtual Status Status { get; set; }
        public virtual Author Author { get; set; }
        public virtual SchoolLevel SchoolLevel { get; set; }
        public virtual ICollection<Patient> Patients { get; set; }


    }

这是我的病人表

  public class Patient
    {
        [Key]
        public int PatientId { get; set; }
        public int PersonId { get; set; }
        public int Record { get; set; }
        [Display(Name = "Fecha de Ingreso")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime? CreationDate { get; set; }
        [Display(Name = "Seguro Principal")]
        public int InsuranceId { get; set; }
        [Display(Name = "Tipificacion Sanguinea")]
        public int BloodTypeId { get; set; }
        [Display(Name = "Edad")]
        [MaxLength(10, ErrorMessage = "La longitud maxima del campo es {1} caracteres")]
        public string Age { get; set; }
        public virtual Person Person { get; set; }
        public virtual BloodType BloodType { get; set; }
        public virtual Insurance Insurance { get; set; }
       } 

这是我正在使用的创建帖子,如果你不是要改进的东西

      public async Task<ActionResult> Create(PatientView view)
           {
                    var people = ToPeople(view);     
                    people.StatusId = 1;
                    _db.People.Add(people);
                    _db.SaveChanges();
                    var patient = ToPatient(view);
                    patient.PersonId = people.PersonId;                            
                    _db.Patients.Add(patient);    
                    await _db.SaveChangesAsync();
           }

0 个答案:

没有答案