Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing Model to View
    text
    copied!<p>So I'm passing the following view model to my view, but the view is throwing an exception everytime it I try to visit the page. Any explanation as to why this is happening would be great, pointers on how to fix it would be better! Thanks.</p> <pre><code>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]'. </code></pre> <p>View Model</p> <pre><code>public class CategoryIndex { public string Title { get; set; } [DisplayName("Categories")] public IEnumerable&lt;string&gt; CategoryNames { get; set; } } </code></pre> <p>View</p> <pre><code>@model IEnumerable&lt;MyCompareBase.Models.CategoryIndex&gt; @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } &lt;h2&gt;Index&lt;/h2&gt; &lt;p&gt; @Html.ActionLink("Create New", "Create") &lt;/p&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt; @Html.DisplayNameFor(model =&gt; model.Title) &lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; @foreach (var item in Model) { &lt;tr&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.CategoryNames) &lt;/td&gt; &lt;td&gt; @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) | @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) | @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ }) &lt;/td&gt; &lt;/tr&gt; </code></pre> <p>}</p> <p></p> <p>Controller</p> <pre><code>public ActionResult Index() { var localDb = db.Categories.Select(c =&gt; c.Name); var wcf = category.Categories().Select(c =&gt; c.Name); var all = new HashSet&lt;String&gt;(localDb); all.UnionWith(wcf); var viewModel = new Models.CategoryIndex { Title = "Avaliable Product Categories", CategoryNames = all.AsEnumerable() }; return View(viewModel); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload