Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my statically typed partial view trying to render an unintended model type?
    primarykey
    data
    text
    <p>I have a partial view, in my ASP.NET MVC4 web app. It's just a partial view designed to display a table, given a collection of entities. It looks like this:</p> <pre><code>@model ICollection&lt;Portal.Models.Matter&gt; &lt;table class="table table-striped table-bordered table-hover"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Last Accessed&lt;/th&gt; &lt;th&gt;Client&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; @if (Model.Count &gt; 0) { @Html.DisplayForModel() } else { &lt;tr&gt; &lt;td colspan="3" class="text-muted text-centered"&gt;There are no Matters to display.&lt;/td&gt; &lt;/tr&gt; } &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>I have a DisplayTemplate for <code>Matter</code> that is statically typed to the single <code>Matter</code> entity, and it handles rendering of each table row.</p> <p>For some reason, when this partial is rendered, instead of using the <code>Matter</code> DisplayTemplate it just shows the following:</p> <pre><code>System.Collections.Generic.List`1[Portal.Models.Account]System.Collections.Generic.List`1[Portal.Models.Account] </code></pre> <p>Now, this partial is not even bound to a collection of <code>Account</code> entities. It's bound to a collection of <code>Matter</code> entities. So, why is <code>@Html.DisplayForModel()</code> trying to display for a collection of <code>Account</code>s? At run time, using a debugger, I can see the <code>Model</code> is in fact a collection of <code>Matter</code> entities, not <code>Account</code>.</p> <p>I render this partial using the following code:</p> <pre><code>@Html.Partial("~/Views/Matter/_Table.cshtml", Model.Client.Matters, new ViewDataDictionary()) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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