Модель: (имя файла: Class1.cs)
namespace Project
{
public class Class1
{
// attributes defined : title, date etc
}
public class Class1DBContext : DbContext
{
public DbSet<Class1> Class1Table{ get; set; }
}
public class Class2
{
// attributes defined : Name, Location etc
}
public class Class2DBContext : DbContext
{
public DbSet<Class2> Class2Table{ get; set; }
}
public class ParentView
{
public Class1 Class1{ get; set; }
public Class2 Class2{ get; set; }
}
}
В файле Class1Controller.cs:
namespace Project.Controllers
{
public class Class1Controller : Controller
{
private Class1DBContext db = new Class1DBContext();
public ViewResult Index()
{
return View(db.Class1Table.ToList());
}
}
В Index.cshtml:
Index view:
@model IEnumerable<Project.Models.ParentView>
@{
ViewBag.Title = "List";
}
<div style="float:left;">
<h1 style="color:Black;" >List</h1>
</div>
<div>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Class1.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Class1.Description)
</td>
</tr>
}
</div>
<div>
@{Html.RenderPartial("_PartialNew");}
</div>
в коде частичного представления (partNew.cshtml):
@model IEnumerable<Project.Models.ParentView>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Class2.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Class2.Location)
</td>
</tr>
}
Сообщение об ошибке:
Элемент модели, переданный в словарь, имеет тип 'System.Collections.Generic.List 1[Project.Models.Class1]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1 [Project.Models.ParentView].