Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Replace the <code>foreach</code> loop in your view with a call to an editor template:</p> <pre><code>@model IEnumerable&lt;MyProgramRatingApp.Program&gt; @{ ViewBag.Title = "CastVote"; } &lt;h2&gt;CastVote&lt;/h2&gt; @using (Html.BeginForm()) { &lt;table&gt; &lt;tr&gt; &lt;th style="border:1px solid Teal; background-color:Gray"&gt; ProgramName &lt;/th&gt; &lt;th style="border:1px solid Teal; background-color:Gray"&gt; RatingID &lt;/th&gt; &lt;th style="border:1px solid Teal; background-color:Gray"&gt; ProgramCategory &lt;/th&gt; &lt;th style="border:1px solid Teal; background-color:Gray"&gt;&lt;/th&gt; &lt;/tr&gt; @Html.EditorForModel() &lt;/table&gt; &lt;p&gt; &lt;input type="submit" value="Cast Vote" /&gt; &lt;/p&gt; } </code></pre> <p>and then define the editor template which will be automatically rendered for each element in the model (<code>~/Views/Shared/EditorTemplates/Program.cshtml</code>):</p> <pre><code>@model MyProgramRatingApp.Program &lt;tr&gt; &lt;td style="border:1px solid Teal"&gt; @Html.EditorFor(x =&gt; x.ProgramName) &lt;/td&gt; &lt;td style="border:1px solid Teal"&gt; @Html.EditorFor(x =&gt; x.RatingID) &lt;/td&gt; &lt;td style="border:1px solid Teal"&gt; @Html.EditorFor(x =&gt; x.ProgramCategory) &lt;/td&gt; &lt;td style="border:1px solid Teal"&gt; @Html.DropDownListFor( x =&gt; x.RatingID, (SelectList)ViewBag.RatingEnum, String.Empty ) &lt;/td&gt; &lt;/tr&gt; </code></pre> <p>Notice that I have used <code>@Html.EditorFor</code> in the editor template instead of <code>@Html.DisplayFor</code> in order to generate input fields. If you don't do that you won't get any values back in the controller because your form doesn't contain any input elements. If you don't want to show input fields you could use hidden inputs:</p> <pre><code>@model MyProgramRatingApp.Program &lt;tr&gt; &lt;td style="border:1px solid Teal"&gt; @Html.DisplayFor(x =&gt; x.ProgramName) @Html.HiddenFor(x =&gt; x.ProgramName) &lt;/td&gt; &lt;td style="border:1px solid Teal"&gt; @Html.DisplayFor(x =&gt; x.RatingID) @Html.HiddenFor(x =&gt; x.RatingID) &lt;/td&gt; &lt;td style="border:1px solid Teal"&gt; @Html.DisplayFor(x =&gt; x.ProgramCategory) @Html.HiddenFor(x =&gt; x.ProgramCategory) &lt;/td&gt; &lt;td style="border:1px solid Teal"&gt; @Html.DropDownListFor( x =&gt; x.RatingID, (SelectList)ViewBag.RatingEnum, String.Empty ) &lt;/td&gt; &lt;/tr&gt; </code></pre> <p>The editor template will generate correct names for the input fields so that the model binder correctly binds the values.</p> <p>You may also take a look at the <a href="http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx" rel="nofollow">following article</a> to better understand the wire format that is expected for collections.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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