【发布时间】:2013-12-16 17:52:35
【问题描述】:
所以我将以下视图模型传递给我的视图,但每次我尝试访问该页面时,视图都会引发异常。关于为什么会发生这种情况的任何解释都会很好,关于如何解决它的指针会更好!谢谢。
The model item passed into the dictionary is of type
MyCompareBase.Models.CategoryIndex', but this dictionary requires a model item
of type
'System.Collections.Generic.IEnumerable`1[MyCompareBase.Models.CategoryIndex]'.
查看模型
public class CategoryIndex
{
public string Title { get; set; }
[DisplayName("Categories")]
public IEnumerable<string> CategoryNames { get; set; }
}
查看
@model IEnumerable<MyCompareBase.Models.CategoryIndex>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CategoryNames)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
控制器
public ActionResult Index()
{
var localDb = db.Categories.Select(c => c.Name);
var wcf = category.Categories().Select(c => c.Name);
var all = new HashSet<String>(localDb);
all.UnionWith(wcf);
var viewModel = new Models.CategoryIndex
{
Title = "Avaliable Product Categories",
CategoryNames = all.AsEnumerable()
};
return View(viewModel);
}
【问题讨论】:
标签: c# .net asp.net-mvc view model