简体   繁体   English

一个视图中的ASP.NET MVC多个模型

[英]ASP.NET MVC multiple models in one view

I have the following models Patient Class: 我有以下型号的患者舱:

public class Patient
{
    public int PatientID { get; set; }

    public virtual Salutation salutation { get; set; }
    public int SalutationID { get; set; }
    public string Surname { get; set; }
    public string Firstname { get; set; }
    [Display(Name = "Date of Birth")]
    [DisplayFormat(DataFormatString="{0:d}", ApplyFormatInEditMode=true)]
    public DateTime DOB { get; set; }
    public string RegNo { get; set; }
    public DateTime RegDate { get; set; }
    public string Occupation { get; set; }
}

VatalSigns Class VatalSigns类

public class VitalSign
{
    public int VitalSignID { get; set; }
    public string Sign { get; set; }
    [Display(Name = "Lower Limit")]
    public int? LowerHold { get; set; }
    [Display(Name = "Upper Limit")]
    public int? UpperHold { get; set; }
    [Display(Name = "Unit Of Measurment")]
    public string Units { get; set; }
}

PV class that stores VitalSigns for each patient 为每个患者存储VitalSigns的PV类

public class PVSign
{

    public long PVSignId { get; set; }
    [Display(Name = "Patient")]
    public int PatientID { get; set; }
    public VitalSign VitalSigns { get; set; }
    //public IList<VitalSign> VitalSigns { get; set; }
    public Patient patient { get; set; }

}

Now the problem I have is that I have not been able to get display on one from to enter the details. 现在,我遇到的问题是我无法从中获得显示的详细信息。 I want to select the Patient and the different Vitals signs will appear and I will save the ones I need to the PVSign table. 我要选择患者,然后将出现不同的生命体征,并将需要的体征保存到PVSign表中。

I have tired all sorts of samples from the web. 我已经厌倦了网上的各种样本。 You can see from the code below, this is the index stub: 您可以从下面的代码中看到这是索引存根:

public ActionResult Index()
{
    var pVSigns = db.PVSigns.Include(p => p.patient).Include(p => p.PVSignId).Include(p => p.VitalSigns);
    //var pVSigns = from o in db.Patients join o2 in db.PVSigns
    //List<object> myModel = new List<object>();
    //myModel.Add(db.Patients.ToList());
    //myModel.Add(db.VitalSigns.ToList());

    //return View(myModel);
    return View(pVSigns.ToList());
}

How to I solve this issue. 我该如何解决这个问题。 I am new to MVC, if it was Webforms, I would have been through with this project. 我是MVC的新手,如果它是Webforms,那么我会经历这个项目。

Thank you. 谢谢。

There is no single answer(solution) to this(your) problem. 这个问题没有一个答案。 It depends on how you want to design/build/achieve your solution. 这取决于您要如何设计/构建/实现解决方案。 The simple, brute solution : Just have a view model as a wraper that has as his properties the classes(models) you made and work around that, 简单,简单的解决方案:只需将一个视图模型作为包装器,将您制作的类(模型)作为其属性,然后加以解决,

public class FullPatientDetailsViewModel
{
    public Patient { get; set;}
    public List<PVSign> PatientPvSigns { get; set;} // Patien PV Signs
}

Or use just a PatientViewModel and load his PVSigns async with Ajax. 或者仅使用PatientViewModel并通过Ajax异步加载他的PVSigns。 There is no simple best solution, it all depends about what do you want to achieve as a bigger picture. 没有简单的最佳解决方案,这全都取决于您想要从更大的角度实现什么。

I've answered a similar question here . 我在这里回答了类似的问题。 As Alexandru mentioned, the easiest and most straightforward way would be to wrap your models inside a ViewModel . 如Alexandru所述,最简单,最直接的方法是将模型包装在ViewModel You can read about other ways to implement this here . 您可以在此处阅读有关实现此方法的其他方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM