[英]model item passed into the dictionary is of type 'System.Collections.Generic.List, but this dictionary requires a model item of type
I have an ASP.NET MVC 5 app where users can see an index of projects. 我有一个ASP.NET MVC 5应用程序,用户可以在其中查看项目索引。 I have added another database table called "Applications", which would store application objects that the users create when they apply for a project from the index.
我添加了另一个名为“应用程序”的数据库表,该表将存储用户在从索引中申请项目时创建的应用程序对象。 The Applications model:
应用程序模型:
public class Application
{
public int ApplicationId { get; set; }
public int ProjectId { get; set; }
public string UserId { get; set; }
public string CoverLetter { get; set; }
}
Where ProjectId and UserId are foreign keys (from dbo.Projects and dbo.AspNetUsers respectively) 其中ProjectId和UserId是外键(分别来自dbo.Projects和dbo.AspNetUsers)
Now for the Projects index view I have changed the view model into ApplicationTwoViewModel: 现在,对于Projects索引视图,我已将视图模型更改为ApplicationTwoViewModel:
public class ApplicationTwoViewModel
{
public IEnumerable<Project> Model1 { get; set; }
public Application Model2 { get; set; }
}
The Index.cshtml: Index.cshtml:
@using Leepio.Models
@using Microsoft.AspNet.Identity
@model ApplicationTwoViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create" ,"Projects")
</p>
<table class="table">
<tr>
<th>
@Html.ActionLink("Title", "Index", "Projects", new {SortOrder = (ViewBag.SortOrder==null?"Asc":(ViewBag.SortOrder=="Asc"?"Desc":"Asc")), SortBy = "Title"})
</th>
<th>
@Html.ActionLink("Application Deadline", "Index", "Projects", new {SortOrder = (ViewBag.SortOrder == null ? "Asc" : (ViewBag.SortOrder == "Asc" ? "Desc" : "Asc")), SortBy = "ApplicationDeadline"}) <br/>
@Html.ActionLink("Hourly Rate (DKK)", "Index", "Projects", new { SortOrder = (ViewBag.SortOrder == null ? "Asc" : (ViewBag.SortOrder == "Asc" ? "Desc" : "Asc")), SortBy = "HourlyRate" })
</th>
<th>
@Html.ActionLink("Skill requirements", "Index", "Projects", new { SortOrder = (ViewBag.SortOrder == null ? "Asc" : (ViewBag.SortOrder == "Asc" ? "Desc" : "Asc")), SortBy = "RequiredSkills" })
</th>
</tr>
@foreach (var item in Model.Model1) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.ApplicationDeadline)<br/>
@Html.DisplayFor(modelItem => item.HourlyRate)
</td>
<td>
@Html.DisplayFor(modelItem => item.RequiredSkills)
</td>
<td>
@if(item.UserId == User.Identity.GetUserId())
{
@Html.ActionLink("Edit", "Edit", "Projects", new {id = item.ProjectId})
@Html.ActionLink("Delete", "Delete", "Projects", new {id = item.ProjectId})
}
@Html.ActionLink("Details", "Details", "Projects", new {id = item.ProjectId}) |
@Html.ActionLink("Apply", "Index", "Applications", new { id = item.ProjectId }) |
</td>
</tr>
}
</table>
@{
double TotalPage = @ViewBag.TotalPages;
}
<ul class="pagination">
@for (int i = 1; i <= TotalPage; i++)
{
if (i == ViewBag.Page)
{
<li class="active"> @Html.ActionLink(i.ToString() + " ", "Index", "Projects", new { SortOrder = (ViewBag.SortOrder == null ? "Asc" : ViewBag.SortOrder), SortBy = (ViewBag.SortBy == null ? "Title" : ViewBag.SortBy), Page = i })</li>
}
else
{
<li>
@Html.ActionLink(i.ToString() + " ", "Index", "Projects", new { SortOrder = (ViewBag.SortOrder == null ? "Asc" : ViewBag.SortOrder), SortBy = (ViewBag.SortBy == null ? "Title" : ViewBag.SortBy), Page = i })
</li>
}
}
</ul>
And of course I am using Model.Model1 and Model.Model2. 当然,我正在使用Model.Model1和Model.Model2。
When trying to open the Projects index view I get the following: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Leepio.Models.Project]', but this dictionary requires a model item of type 'Leepio.Models.ApplicationTwoViewModel'. 尝试打开Projects索引视图时,我得到以下信息: 传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [Leepio.Models.Project]',但是此字典需要的模型项为键入“ Leepio.Models.ApplicationTwoViewModel”。
ProjectsController: ProjectsController:
public ActionResult Index(string SortOrder, string SortBy, string Page)
{
ViewBag.SortOrder = SortOrder;
ViewBag.SortBy = SortBy;
var projects = db.Projects.ToList();
var model = new ApplicationTwoViewModel
{
Model1 = new List<Project>(projects),
Model2 = new Application
{
UserId = User.Identity.GetUserId(),
ProjectId = 11,
CoverLetter = "asdf",
ApplicationId = 23,
}
};
//bunch of code I cut out regarding the sorting
ViewBag.TotalPages = Math.Ceiling(db.Projects.ToList().Count()/10.0);
int page = int.Parse(Page == null ? "1" : Page);
ViewBag.Page = page;
projects = projects.Skip((page - 1) * 10).Take(10).ToList();
return View(projects);
}
I've tried initializing Model2 hard coded even though I do not want it this way. 我已经尝试初始化Model2硬编码,即使我不想这样。 What could solve this error?
什么可以解决此错误? I have a feeling the initialization is off but I am not sure
我感觉初始化已关闭,但不确定
Your View expects you to accept an instace of your ApplicationTwoViewModel
class as seen at the top of your Index.cshtml
View: 您的View希望您接受
ApplicationTwoViewModel
类的实例,如Index.cshtml
View顶部所示:
@model ApplicationTwoViewModel
So you just need to create one and pass it in, which should be quite easy as you are already building it within your Controller Action : 因此,您只需要创建一个并传递它,这应该非常容易,因为您已经在Controller Action中构建了它:
// Pass the model to your View
return View(model);
Since you are explicitly editing it after initializing it, you'll probably want to perform the following before your return
call and well as ensuring that any other operations (ie sorting and filtering) are done to the collection within your Model as well : 由于您是在初始化后显式地对其进行编辑,因此您可能要在
return
调用之前执行以下操作,并确保对Model中的集合也进行了其他任何操作(即排序和过滤):
// Perform your paging at the model level
model.Model1 = model.Model1.Skip((page - 1) * 10).Take(10).ToList();
// Now return it
return View(model);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.