![](/img/trans.png)
[英]Dictionary requires a model item of type System.Collections.Generic.List
[英]model item passed into the dictionary is of type 'System.Collections.Generic.List, but this dictionary requires a model item of type
我有一個ASP.NET MVC 5應用程序,用戶可以在其中查看項目索引。 我添加了另一個名為“應用程序”的數據庫表,該表將存儲用戶在從索引中申請項目時創建的應用程序對象。 應用程序模型:
public class Application
{
public int ApplicationId { get; set; }
public int ProjectId { get; set; }
public string UserId { get; set; }
public string CoverLetter { get; set; }
}
其中ProjectId和UserId是外鍵(分別來自dbo.Projects和dbo.AspNetUsers)
現在,對於Projects索引視圖,我已將視圖模型更改為ApplicationTwoViewModel:
public class ApplicationTwoViewModel
{
public IEnumerable<Project> Model1 { get; set; }
public Application Model2 { get; set; }
}
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>
當然,我正在使用Model.Model1和Model.Model2。
嘗試打開Projects索引視圖時,我得到以下信息: 傳遞到字典中的模型項的類型為'System.Collections.Generic.List`1 [Leepio.Models.Project]',但是此字典需要的模型項為鍵入“ Leepio.Models.ApplicationTwoViewModel”。
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);
}
我已經嘗試初始化Model2硬編碼,即使我不想這樣。 什么可以解決此錯誤? 我感覺初始化已關閉,但不確定
您的View希望您接受ApplicationTwoViewModel
類的實例,如Index.cshtml
View頂部所示:
@model ApplicationTwoViewModel
因此,您只需要創建一個並傳遞它,這應該非常容易,因為您已經在Controller Action中構建了它:
// Pass the model to your View
return View(model);
由於您是在初始化后顯式地對其進行編輯,因此您可能要在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.