【发布时间】:2015-09-14 11:23:26
【问题描述】:
抱歉,如果这已经得到解答,但我正在寻找处理以下情况时的最佳做法。
我有一个相当大的 MVC5 应用程序,其中包含许多表单/页面。
对于这个例子,假设我有一个像下面这样的病人类(这是一个更大类的精简版)
Public Class Patient {
[Display(Name = "Patient Trial Number")]
public int ID { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Patient DOB")]
public DateTime PatientDOB { get; set; }
[Required]
[Display(Name = "Patient Gender")]
public Gender PatientGender { get; set; }
}
对于需要完成的每个表格,我都有不同的模型。其中一些是非常大的 80 多个属性。
以下是表单模型的精简示例。
public class ExamplePatientForm
{
[Key]
public Patient PatientID { get; set; }
[Required]
[Display(Name = "Was Sample One taken?")]
public bool? SampleOneTaken{ get; set; }
[Required]
[Display(Name = "Date Sample One Taken")]
public DateTime DateSampleOneTaken { get; set; }
[Required]
[Display(Name = "Was Sample Two taken?")]
public bool? SampleTwoTaken{ get; set; }
[Required]
[Display(Name = "Date Sample Two Taken")]
public DateTime DateSampleTwoTaken { get; set; }
public int PatientRating {get;set;}
public string Comments { get; set; }
}
实际上,患者类和个体表单类都大得多。
最初我使用 html.HiddenFor 将 Patient 类的详细信息保存在各个表单中,尽管这感觉不对。
然后我创建了 ViewModel(见下文)
[Key]
public int PatientID { get; set; }
[Required]
[Display(Name = "Was Sample One taken?")]
public bool? SampleOneTaken{ get; set; }
[Required]
[Display(Name = "Date Sample One Taken")]
public DateTime DateSampleOneTaken { get; set; }
[Required]
[Display(Name = "Was Sample Two taken?")]
public bool? SampleTwoTaken{ get; set; }
[Required]
[Display(Name = "Date Sample Two Taken")]
public DateTime DateSampleTwoTaken { get; set; }
public int PatientRating {get;set;}
public string Comments { get; set; }
}
ViewModel 删除了关系并将 PatientID 存储为整数。然后我将其映射回控制器中的实体模型。
虽然必须复制每个表单模型来创建视图模型对我来说似乎有点疯狂,尤其是因为有些包含 80 多个属性。
有谁知道解决这个问题的最佳方法?我将有大约 50 个独特的表格。
我还为患者模型创建了一个编辑器模板,这样我就可以在每个表格的顶部显示一些患者信息。但只需要显示某些属性,所以不确定我是否需要为此创建单独的 PatientViewModel 或只是隐藏其他元素。
希望这是有道理的。 任何帮助将不胜感激。
问候, 抢
【问题讨论】:
-
您可以做的一件事是将您的 VERY LARGE 模型划分为小的 View Model,然后使用向导创建/更新患者。
-
并显示您可以使用选项卡的信息 - 在顶部显示常用信息和选项卡中的其他信息。 - 每个选项卡都可以使用 Ajax 拥有自己的视图模型加载数据。
标签: c# asp.net-mvc model-view-controller asp.net-mvc-viewmodel