C# MVC 3实体框架-跨多个视图进行渐进式模型更新
全部, 我有一个应用程序(非常类似于一个渐进式调查应用程序),包含一系列表单,跨越多个视图,逐步为其模型(NewClub)构建数据。例如,在第1页,插入/更新3个“新俱乐部”模型属性,然后在第2页,插入/更新3个或4个以上的“新俱乐部”模型属性,等等 由于并非每个页面上都表示模型中的所有属性,因此当我在模型类中打开C# MVC 3实体框架-跨多个视图进行渐进式模型更新,c#,asp.net-mvc-3,entity-framework,C#,Asp.net Mvc 3,Entity Framework,全部, 我有一个应用程序(非常类似于一个渐进式调查应用程序),包含一系列表单,跨越多个视图,逐步为其模型(NewClub)构建数据。例如,在第1页,插入/更新3个“新俱乐部”模型属性,然后在第2页,插入/更新3个或4个以上的“新俱乐部”模型属性,等等 由于并非每个页面上都表示模型中的所有属性,因此当我在模型类中打开[Required(ErrorMessage=”“)]数据注释时,模型状态始终无效,因为并非所有页面上都存在模型中所需的属性 逐步更新我的“新俱乐部”模式的最佳方式是什么 谢谢 模型
[Required(ErrorMessage=”“)]
数据注释时,模型状态始终无效,因为并非所有页面上都存在模型中所需的属性
逐步更新我的“新俱乐部”模式的最佳方式是什么
谢谢
模型
新俱乐部模型课程
public class NewClub
{
public NewClub()
{
//Related tables here
NewClubBuilders = new List<NewClubBuilder>();
NewClubSponsors = new List<NewClubSponsor>();
NewClubCaseQuestions = new List<NewClubCaseQuestion>();
NewClubCaseAnswers = new List<NewClubCaseAnswer>();
NewClubKitOrders = new List<NewClubKitOrder>();
NewClubCommunityLeaders = new List<NewClubCommunityLeader>();
NewClubRecruitingTeams = new List<NewClubRecruitingTeam>();
NewClubRecruitingTeamDates = new List<NewClubRecruitingTeamDate>();
NewClubRecruiterTrainingSchedules = new List<NewClubRecruiterTrainingSchedule>();
NewClubProspects = new List<NewClubProspect>();
}
//Primary Key
public int Id { get; set; }
//Club Info
public string CreatorMasterCustomerId { get; set; }
public string ClubMasterCustomerId { get; set; }
public string District { get; set; }
public string Division { get; set; }
public bool IsConverted { get; set; }
[Display(Name = "Name your club")]
[Required(ErrorMessage = "Please provide your club with a name.")]
public string NewClubName { get; set; }
[Display(Name = "Location of your club")]
[Required(ErrorMessage = "Please provide your club location.")]
public string NewClubLocation { get; set; }
public string NewClubType { get; set; }
//Lt Governor
public string LtGovMasterCustomerId { get; set; }
public string LtGovContact { get; set; }
public string LtGovEmail { get; set; }
public string LtGovPhone { get; set; }
//Club Counselor
public string ClubCounselorMasterCustomerId { get; set; }
[Display(Name = "Club counselor")]
[Required(ErrorMessage = "Club counselor name")]
public string ClubCounselorContact { get; set; }
[Display(Name = "Club counselor email")]
[Required(ErrorMessage = "Club counselor email")]
public string ClubCounselorEmail { get; set; }
[Display(Name = "Club counselor phone")]
[Required(ErrorMessage = "Club counselor phone")]
public string ClubCounselorPhone { get; set; }
//Build Progress
public bool LetsGetStartedComplete { get; set; }
public bool FormANewClubTeamComplete { get; set; }
public bool ResourcesComplete { get; set; }
public bool CaseForANewClubComplete { get; set; }
public bool SubmitSiteSurveyComplete { get; set; }
public bool NewClubBuildingKitComplete { get; set; }
public bool StatsComplete { get; set; }
public bool BuildScheduleComplete { get; set; }
public bool ProspectsComplete { get; set; }
public bool MembersComplete { get; set; }
public bool EmailComplete { get; set; }
public bool ClubOrganizationChecklistComplete { get; set; }
public bool NewClubInformationComplete { get; set; }
public bool PermanentRosterComplete { get; set; }
public bool SubmitCharterPaymentComplete { get; set; }
public bool ClubByLawsComplete { get; set; }
//Date stuff
public DateTime? DateCreated { get; set; }
public DateTime? DateUpdated { get; set; }
// Related tables here
public virtual List<NewClubBuilder> NewClubBuilders { get; set; }
public virtual List<NewClubSponsor> NewClubSponsors { get; set; }
public virtual List<NewClubCaseQuestion> NewClubCaseQuestions { get; set; }
public virtual List<NewClubCaseAnswer> NewClubCaseAnswers { get; set; }
public virtual List<NewClubKitOrder> NewClubKitOrders { get; set; }
public virtual List<NewClubCommunityLeader> NewClubCommunityLeaders { get; set; }
public virtual List<NewClubRecruitingTeam> NewClubRecruitingTeams { get; set; }
public virtual List<NewClubRecruitingTeamDate> NewClubRecruitingTeamDates { get; set; }
public virtual List<NewClubRecruiterTrainingSchedule> NewClubRecruiterTrainingSchedules { get; set; }
public virtual List<NewClubProspect> NewClubProspects { get; set; }
}
public class LetsGetStartedViewModel
{
public NewClub NewClub { get; set; }
public bool HasExistingBuildingClubs { get; set; }
}
public class FormANewClubTeamViewModel
{
public NewClub NewClub { get; set; }
//All club sponsors
public List<NewClubSponsor> Sponsors { get; set; }
}
公共类新俱乐部
{
公共俱乐部()
{
//这里有相关的表格
NewClubBuilders=新列表();
NewClubSponsors=新列表();
NewClubCaseQuestions=新列表();
NewClubCaseAnswers=新列表();
newclubkitorers=新列表();
NewClubCommunityLeaders=新列表();
NewClubRecruitingTeams=新列表();
NewClubRecruitingTeamDates=新列表();
NewClubRecruiterTrainingSchedules=新列表();
NewClubProspects=新列表();
}
//主键
公共int Id{get;set;}
//俱乐部信息
公共字符串CreatorMasterCustomerId{get;set;}
公共字符串ClubMasterCustomerId{get;set;}
公共字符串区域{get;set;}
公共字符串除法{get;set;}
公共布尔是转换的{get;set;}
[显示(Name=“命名您的俱乐部”)]
[必需(ErrorMessage=“请为您的俱乐部提供名称。”)]
公共字符串NewClubName{get;set;}
[显示(Name=“您俱乐部的位置”)]
[必需(ErrorMessage=“请提供您的俱乐部位置。”)]
公共字符串NewClubLocation{get;set;}
公共字符串NewClubType{get;set;}
//副州长
公共字符串LtGovMasterCustomerId{get;set;}
公共字符串LtGovContact{get;set;}
公共字符串LtGovEmail{get;set;}
公共字符串LtGovPhone{get;set;}
//俱乐部顾问
公共字符串ClubCounselorMasterCustomerId{get;set;}
[显示(Name=“俱乐部顾问”)]
[必需(ErrorMessage=“俱乐部顾问姓名”)]
公共字符串ClubCounselorContact{get;set;}
[显示(Name=“俱乐部顾问电子邮件”)]
[必需(ErrorMessage=“俱乐部顾问电子邮件”)]
公共字符串ClubCounselorEmail{get;set;}
[显示(Name=“俱乐部顾问电话”)]
[必需(ErrorMessage=“俱乐部顾问电话”)]
公共字符串ClubCounselorPhone{get;set;}
//建立进步
公共bool-LetsGetStartedComplete{get;set;}
public bool FormANewClubTeamComplete{get;set;}
公共布尔资源完成{get;set;}
NewClubComplete{get;set;}的公共布尔案例
public bool SubmitSiteSurveyComplete{get;set;}
public bool NewClubBuildingKitComplete{get;set;}
公共bool statcomplete{get;set;}
公共布尔BuildScheduleComplete{get;set;}
公共bool ProspectsComplete{get;set;}
公共bool成员完成{get;set;}
公共bool EmailComplete{get;set;}
public bool cluborOrganizationCheckListComplete{get;set;}
public bool NewClubInformationComplete{get;set;}
公共布尔永久名册完成{get;set;}
公共bool SubmitCharterPaymentComplete{get;set;}
公共bool ClubByLawsComplete{get;set;}
//约会用品
公共日期时间?DateCreated{get;set;}
公共日期时间?日期更新{get;set;}
//这里有相关的表格
公共虚拟列表NewClubBuilders{get;set;}
公共虚拟列表NewClubSponsors{get;set;}
公共虚拟列表NewClubCaseQuestions{get;set;}
公共虚拟列表NewClubCaseAnswers{get;set;}
公共虚拟列表NewClubEditor{get;set;}
公共虚拟列表NewClubCommunityLeaders{get;set;}
公共虚拟列表NewClubRecruitingTeams{get;set;}
公共虚拟列表NewClubRecruitingTeamDates{get;set;}
公共虚拟列表NewClubRecruiterTrainingSchedules{get;set;}
公共虚拟列表NewClubProspects{get;set;}
}
视图模型
public class NewClub
{
public NewClub()
{
//Related tables here
NewClubBuilders = new List<NewClubBuilder>();
NewClubSponsors = new List<NewClubSponsor>();
NewClubCaseQuestions = new List<NewClubCaseQuestion>();
NewClubCaseAnswers = new List<NewClubCaseAnswer>();
NewClubKitOrders = new List<NewClubKitOrder>();
NewClubCommunityLeaders = new List<NewClubCommunityLeader>();
NewClubRecruitingTeams = new List<NewClubRecruitingTeam>();
NewClubRecruitingTeamDates = new List<NewClubRecruitingTeamDate>();
NewClubRecruiterTrainingSchedules = new List<NewClubRecruiterTrainingSchedule>();
NewClubProspects = new List<NewClubProspect>();
}
//Primary Key
public int Id { get; set; }
//Club Info
public string CreatorMasterCustomerId { get; set; }
public string ClubMasterCustomerId { get; set; }
public string District { get; set; }
public string Division { get; set; }
public bool IsConverted { get; set; }
[Display(Name = "Name your club")]
[Required(ErrorMessage = "Please provide your club with a name.")]
public string NewClubName { get; set; }
[Display(Name = "Location of your club")]
[Required(ErrorMessage = "Please provide your club location.")]
public string NewClubLocation { get; set; }
public string NewClubType { get; set; }
//Lt Governor
public string LtGovMasterCustomerId { get; set; }
public string LtGovContact { get; set; }
public string LtGovEmail { get; set; }
public string LtGovPhone { get; set; }
//Club Counselor
public string ClubCounselorMasterCustomerId { get; set; }
[Display(Name = "Club counselor")]
[Required(ErrorMessage = "Club counselor name")]
public string ClubCounselorContact { get; set; }
[Display(Name = "Club counselor email")]
[Required(ErrorMessage = "Club counselor email")]
public string ClubCounselorEmail { get; set; }
[Display(Name = "Club counselor phone")]
[Required(ErrorMessage = "Club counselor phone")]
public string ClubCounselorPhone { get; set; }
//Build Progress
public bool LetsGetStartedComplete { get; set; }
public bool FormANewClubTeamComplete { get; set; }
public bool ResourcesComplete { get; set; }
public bool CaseForANewClubComplete { get; set; }
public bool SubmitSiteSurveyComplete { get; set; }
public bool NewClubBuildingKitComplete { get; set; }
public bool StatsComplete { get; set; }
public bool BuildScheduleComplete { get; set; }
public bool ProspectsComplete { get; set; }
public bool MembersComplete { get; set; }
public bool EmailComplete { get; set; }
public bool ClubOrganizationChecklistComplete { get; set; }
public bool NewClubInformationComplete { get; set; }
public bool PermanentRosterComplete { get; set; }
public bool SubmitCharterPaymentComplete { get; set; }
public bool ClubByLawsComplete { get; set; }
//Date stuff
public DateTime? DateCreated { get; set; }
public DateTime? DateUpdated { get; set; }
// Related tables here
public virtual List<NewClubBuilder> NewClubBuilders { get; set; }
public virtual List<NewClubSponsor> NewClubSponsors { get; set; }
public virtual List<NewClubCaseQuestion> NewClubCaseQuestions { get; set; }
public virtual List<NewClubCaseAnswer> NewClubCaseAnswers { get; set; }
public virtual List<NewClubKitOrder> NewClubKitOrders { get; set; }
public virtual List<NewClubCommunityLeader> NewClubCommunityLeaders { get; set; }
public virtual List<NewClubRecruitingTeam> NewClubRecruitingTeams { get; set; }
public virtual List<NewClubRecruitingTeamDate> NewClubRecruitingTeamDates { get; set; }
public virtual List<NewClubRecruiterTrainingSchedule> NewClubRecruiterTrainingSchedules { get; set; }
public virtual List<NewClubProspect> NewClubProspects { get; set; }
}
public class LetsGetStartedViewModel
{
public NewClub NewClub { get; set; }
public bool HasExistingBuildingClubs { get; set; }
}
public class FormANewClubTeamViewModel
{
public NewClub NewClub { get; set; }
//All club sponsors
public List<NewClubSponsor> Sponsors { get; set; }
}
公共类LetsGetStartedViewModel
{
public NewClub NewClub{get;set;}
公共布尔有现有的BuildingClubs{get;set;}
}
公共类FormNewClubTeamViewModel
{
public NewClub NewClub{get;set;}
//所有俱乐部赞助商
公共列表发起人{get;set;}
}
一种方法是每个视图使用不同的模型,即使用视图模型,而不是使用一个共享的模型。每个ViewModel
当然应该只包含那些与特定视图相关的属性
然后,在将向导移到第n+1页之前,您的控制器
方法必须临时保存第n页的视图模型
,最后,当用户在最后一页单击提交时,所有数据必须提交到最终的控制器
方法
您可以通过构建一个完整的模型
,其中包含其他视图模型
作为属性来实现这一点。最后一个向导页面的Controller
方法随后建立了Model
并将其提交到Submit按钮的Controller
方法。那么您是否只需重新设置现有viewmodels的任务?例如:公共类FormANewClubTeamViewModel{}。只向视图提供它需要的内容,将其余内容存储到其他地方,直到您需要它为止。