0
因此,我將以下視圖模型傳遞給了我的視圖,但每次嘗試訪問該頁面時視圖都會拋出異常。爲什麼發生這種情況的任何解釋都會很好,如何解決這個問題的指針會更好!謝謝。將模型傳遞到視圖
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);
}
謝謝你的幫助,仍然是.Net新手,但我已經整理出來了。 – user1861156