【发布时间】:2016-05-05 19:08:23
【问题描述】:
我有一个 ASP.NET MVC 5 应用程序,用户可以在其中查看项目索引。我添加了另一个名为“Applications”的数据库表,它将存储用户在从索引中申请项目时创建的应用程序对象。 应用模型:
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。
当尝试打开项目索引视图时,我得到以下信息: 传入字典的模型项的类型为“System.Collections.Generic.List`1[Leepio.Models.Project]”,但此字典需要“Leepio.Models.ApplicationTwoViewModel”类型的模型项。
项目控制器:
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,即使我不希望这样。什么可以解决这个错误?我感觉初始化已关闭,但我不确定
【问题讨论】:
-
这将被否决作为答案,因此作为评论:try return View(model);
-
您将错误的模型返回给您查看 (
return View(projects);)。您需要将var model = new ApplicationTwoViewModel返回到您的视图 (return View(model);) -
事实上,这很简单,我称之为错字......
-
实际上它确实修复了错误,谢谢:) 有时,当您搜索一个小时时,其他人可以立即看到问题。
标签: c# asp.net asp.net-mvc asp.net-mvc-4 dictionary