Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You will have to make your view strongly typed to a collection of <code>specimen</code> which is what you are passing to it from your controller action (<code>IEnumerable&lt;specimen&gt;</code>):</p> <pre><code>&lt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;IEnumerable&lt;WildlifeTropical.Models.specimen&gt;&gt;" %&gt; &lt;% foreach (WildlifeTropical.Models.specimen s Model) { %&gt; &lt;div&gt;&lt;%= Html.Encode(s.Name) %&gt;&lt;/div&gt; &lt;% } %&gt; </code></pre> <p>Notice how the view inherits <code>System.Web.Mvc.ViewPage&lt;IEnumerable&lt;WildlifeTropical.Models.specimen&gt;&gt;</code> and it is now strongly typed to a collection of <code>specimens</code> that you will be able to loop through.</p> <p>This being said, personally I don't like writing foreach loops in my views. They make them look ugly. In this case I would use a display template:</p> <pre><code>&lt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;IEnumerable&lt;WildlifeTropical.Models.specimen&gt;&gt;" %&gt; &lt;%= Html.DisplayForModel() %&gt; </code></pre> <p>and then I would define a display template which will automatically be rendered for each element of the model collection (<code>~/Views/Shared/DisplayTemplates/specimen.ascx</code>):</p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;WildlifeTropical.Models.specimen&gt;" %&gt; &lt;div&gt; &lt;%= Html.DisplayFor(x =&gt; x.Name) %&gt; &lt;/div&gt; </code></pre> <p>See how the <code>specimen.ascx</code> user control is now strongly typed to <code>System.Web.Mvc.ViewUserControl&lt;WildlifeTropical.Models.specimen&gt;</code>. This is because it will be rendered for each specimen of the main view model.</p>
 

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