【发布时间】:2014-10-11 23:46:04
【问题描述】:
我试图让 2 个模型在 1 个视图中显示,但它不起作用。我尝试了很多来自 Google 的不同想法,但到目前为止都没有奏效。
错误列表中没有错误。但是当我启动它时,我得到了这个错误。
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Namespace.Models.Class1]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Namespace.Models.ParentClass]'.
我有一个包含 to 子类的父类。如果我直接在子类中使用@model IEnumerable 它可以工作,但在指向父类时不能。
我做错了什么?
编辑
好的,这些是我的文件。
Model1.cs
public int MyProperty1 { get; set; }
public int MyProperty2 { get; set; }
Model2.cs
public int AnotherProperty1 { get; set; }
public int AnotherProperty2 { get; set; }
ViewModel.cs
public IEnumerable<Model1> Model1 { get; set; }
public IEnumerable<Model2> Model2 { get; set; }
HomeController.cs
private ConnectContext db = new ConnectContext();
public ActionResult Index()
{
var model = from m in db.model select m;
model = db.model.OrderByDescending(m => m.ID);
return View(db.model.ToList());
}
Index.chstml
@model IEnumerable<Namespace.Models.ViewModel>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Model1.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Model2.Title)
</td>
</tr>
}
现在有了这样的文件,我的错误信息是
CS1061: 'System.Collections.Generic.IEnumerable<Namespace.Models.Model1>' does not contain a definition for 'Cover' and no extension method 'Cover' accepting a first argument of type 'System.Collections.Generic.IEnumerable<Namespace.Models.Model1>' could be found (are you missing a using directive or an assembly reference?)
【问题讨论】:
标签: c# asp.net asp.net-mvc