使用从另一个类继承的 ViewModel 的部分视图不起作用

发布于 2024-12-04 11:57:24 字数 1380 浏览 0 评论 0原文

我有一个视图模型,该视图在同一页面上显示多个表单。该视图模型包含将呈现的每个表单的“子视图模型”。每个表单都有自己的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多孤肩上扛 2024-12-11 11:57:24

事实证明,当您将 null 值作为模型参数传递给 Html.RenderPartial 时,MVC 会遇到困难。我的修复如下所示:

public class JobsViewModel {
  public NightShiftForm NightShiftForm { get; set; }
  public DayShiftForm DayShiftForm { get; set; }

  public JobsViewModel() {
    NightShiftForm = new NightShiftForm();
    DayShiftForm = new DayShiftForm();
  }
}

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:

public class JobsViewModel {
  public NightShiftForm NightShiftForm { get; set; }
  public DayShiftForm DayShiftForm { get; set; }

  public JobsViewModel() {
    NightShiftForm = new NightShiftForm();
    DayShiftForm = new DayShiftForm();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文