Tag: 视图模型

为什么我的视图模型不能与实现接口的通用ViewModel绑定? (ASP.NET MVC 3)

我正试图通过我的View传递以下ViewModel的实例: public class CompanyListViewModel where T : ICompany { public IEnumerable CompanyList; public IEnumerable StateList; public CompanyListViewModel(IEnumerable list) { CompanyList = list; } } View采用的方式如下: @model Project.ViewModels.CompanyViewModels.CompanyListViewModel 我的控制器代码将这样的内容传递给View: CompanyListViewModel model = new CompanyListViewModel(_companyService.FilterByCompanyState(state)); ICompanyInListAsUser接口实现ICompany接口的位置。 _companyService.FilterByCompanyState(state))类返回一个IEnumerable Company对象,后者又实现了ICompanyInListAsUser接口。 出于某种原因,访问我的视图时收到以下错误: The model item passed into the dictionary is of type ‘Project.ViewModels.CompanyViewModels.CompanyListViewModel`1[Project.ViewModels.CompanyViewModels.ICompanyInListAsUser]’, but this dictionary requires a model item […]

如何在MVC3中将嵌套的ViewModel从View绑定到Controller?

我正在用C#开发一个ASP.NET MVC 3应用程序,我使用Razor。 我现在正处理一个问题,涉及通过Controller向View传递/接收的ViewModel绑定对象。 让我们说清楚。 我有以下ViewModels: public class ContainerViewModel { public int ContainerId {get; set;} public string ContainerName {get; set;} public List ItemData {get; set;} } public class ItemPostModel { public int ItemId {get; set;} public string ItemName {get; set;} public int ItemValue {get; set;} } ContainerViewModel用于将数据传递给View。 其属性ContainerId和ContainerName仅用于显示目的。 必须使用Form填充List属性。 View看起来像这样(它是简化版): @Model.ContainerName @using (Html.BeginForm()) { @foreach(var […]