我有一个带有集合属性的ViewModel。我希望该集合类型(最初是List<T>
上具有一些其他属性,所以我创建了一个从List<T>
继承的类并定义了它的属性。当我发布到控制器操作时,即使发布的名称对我来说正确,也不会设置其中一个属性(我正在发布的唯一属性)。
列表类:
public class ClassEnrollmentPaymentPlanVMList : List<ClassEnrollmentPaymentPlanVM>
{
public Guid StudentID { get; set; }
public Guid ClassEventID { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
[Required]
public Guid SelectedPricePlanID { get; set; }
}
包含列表的VM(为简洁起见,省略了额外的属性):
public class ClassEnrollmentFormVM
{
public Guid StudentID { get; set; }
public Guid ClassEventID { get; set; }
[Required]
public DateTime? StartDate { get; set; }
[Required]
public DateTime? EndDate { get; set; }
public ClassEnrollmentPaymentPlanVMList AvailablePaymentPlans { get; set; }
}
控制器:
[HttpPost]
public ActionResult GetClassPaymentPlans(ClassEnrollmentFormVM vm)
{
AjaxResponse response = new AjaxResponse();
...
return response.ToContentResult();
}
GetClassPaymentPlans的AJAX帖子(从Chrome开发工具中提取。URL解码):
StudentID: 6267cacc-c5c3-41d8-bd77-65bd8be31b7f
ClassEventID: 20d1d0e3-e6fc-4603-b7cc-8373b3d22527
StartDate: 10/3/2018 12:00:00 AM
EndDate: 5/8/2019 12:00:00 AM
AvailablePaymentPlans[0].PricePlanID: 329f138e-cf9b-427c-b944-75304510caa9
AvailablePaymentPlans[0].ScheduledPayments[0].DueDate: 12/27/2018
AvailablePaymentPlans[1].PricePlanID: c3924757-861f-4456-bde5-782ba2759546
AvailablePaymentPlans[1].ScheduledPayments[0].DueDate: 12/27/2018
AvailablePaymentPlans.SelectedPricePlanID: c3924757-861f-4456-bde5-782ba2759546
AvailablePaymentPlans[2].PricePlanID: 64e622f8-6bec-48c7-96b8-faee3fc17832
AvailablePaymentPlans[2].ScheduledPayments[0].DueDate: 12/27/2018
AvailablePaymentPlans[2].ScheduledPayments[1].DueDate: 1/2/2019
AvailablePaymentPlans[2].ScheduledPayments[2].DueDate: 2/2/2019
AvailablePaymentPlans[2].ScheduledPayments[3].DueDate: 3/5/2019
AvailablePaymentPlans[2].ScheduledPayments[4].DueDate: 4/5/2019
AvailablePaymentPlans[2].ScheduledPayments[5].DueDate: 5/6/2019
但是vm.AvailablePaymentPlans.SelectedPricePlanID
等于{00000000-0000-0000-0000-000000000000}
时达到GetClassEnrollmentForm
。起初,我认为可能不是因为隐藏了元素而未发布,但是您可以在上方看到提交了一个AvailablePaymentPlans.SelectedPricePlanID
键,并且在Visual Studio中可以看到Request.Form也包含该键。默认联编程序是否看到它是列表,而忽略了它是派生类型的事实?有趣的是,在“监视”窗口中或将鼠标悬停在vm上时,Visual Studio也会忽略AvailablePaymentPlans
上的其他属性,并且不显示它们,但是intellisense确实提供了这些属性,就像在ClassEnrollmentForm
上声明了该属性一样:基类List<ClassEnrollmentPaymentPlanVM>
。这是使用.NET Framework 4.6.2