使用从另一个类继承的 ViewModel 的部分视图不起作用
我有一个视图模型,该视图在同一页面上显示多个表单。该视图模型包含将呈现的每个表单的“子视图模型”。每个表单都有自己的 PartialView。
所有这些表单共享一组属性(联系信息),但每个表单都有一组不同的附加属性。所以我认为每个表单类从基类继承是有意义的。但是,当我尝试访问该网址时,出现以下错误。
传入字典的模型项是类型 'MyProject.ViewModels.JobForm',但是这个字典 需要类型的模型项目 'MyProject.ViewModels.NightShiftForm'。
主视图
<%@ Control Language="C#"
Inherits="ViewUserControl<MyProject.ViewModels.JobsViewModel>" %>
<%: Html.Partial("_NightShiftForm", Model.NightShiftForm) %>
<%: Html.Partial("_DayShiftForm", Model.DayShiftForm) %>
“_NightShiftForm”的部分视图
<%@ Control Language="C#"
Inherits="ViewUserControl<MyProject.ViewModels.NightShiftForm>" %>
视图模型和视图表单类
public class JobsViewModel {
public NightShiftForm NightShiftForm { get; set; }
public DayShiftForm DayShiftForm { get; set; }
}
public class JobForm {
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class DayShiftForm : JobForm {
public string PreferredCoffeeBrand { get; set; }
public bool IsMorningPerson { get; set; }
}
public class NightShiftForm : JobForm {
public string PreferredLocation { get; set; }
public string PreferredNights { get; set; }
}
有谁知道为什么会发生这种情况?
I have a view model for a view which displays multiple forms on the same page. This view model contains 'sub-view-models' for each form that will be rendered. Each form has it's own PartialView.
All of these forms share a set of properties (contact info), yet each form has a distinct set of additional properties. So I thought it made sense for each of these form classes to inherit from a base class. However, when I attempt to access the url, I get the following error.
The model item passed into the dictionary is of type
'MyProject.ViewModels.JobForm', but this dictionary
requires a model item of type
'MyProject.ViewModels.NightShiftForm'.
Main View
<%@ Control Language="C#"
Inherits="ViewUserControl<MyProject.ViewModels.JobsViewModel>" %>
<%: Html.Partial("_NightShiftForm", Model.NightShiftForm) %>
<%: Html.Partial("_DayShiftForm", Model.DayShiftForm) %>
Partial View for '_NightShiftForm'
<%@ Control Language="C#"
Inherits="ViewUserControl<MyProject.ViewModels.NightShiftForm>" %>
View Model & Form Classes
public class JobsViewModel {
public NightShiftForm NightShiftForm { get; set; }
public DayShiftForm DayShiftForm { get; set; }
}
public class JobForm {
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class DayShiftForm : JobForm {
public string PreferredCoffeeBrand { get; set; }
public bool IsMorningPerson { get; set; }
}
public class NightShiftForm : JobForm {
public string PreferredLocation { get; set; }
public string PreferredNights { get; set; }
}
Does anyone know why this might be happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,当您将 null 值作为模型参数传递给
Html.RenderPartial
时,MVC 会遇到困难。我的修复如下所示:Turns out that MVC has a hard time when you pass null values as the model parameter to
Html.RenderPartial
. My fix looks like the following: