Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.Net MVC3 Model Binding IEnumerable<T> with Editor Template
    primarykey
    data
    text
    <p>All, please clear up my confusion on how the model binding works with IEnumerables and Editor Templates.</p> <p>I have a view, Approve.cshtml</p> <pre><code>@model IEnumerable&lt;MvcWebsite.Models.Approve&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt; Name &lt;/th&gt; &lt;/tr&gt; @Html.EditorForModel() &lt;/table&gt; </code></pre> <p>A model, Approve.cs</p> <pre><code>public class Approve { public string Name { get;set;} public string Role { get; set; } } </code></pre> <p>And an editor template</p> <pre><code>@model MvcWebsite.Models.Approve @using (Html.BeginForm("Approve", "Registration", FormMethod.Post)) { &lt;tr&gt; &lt;td&gt; @Html.HiddenFor(m =&gt; m.Name) @Html.EditorFor(m =&gt; m.Role) &lt;/td&gt; &lt;td&gt; &lt;input type="submit" value="Approve" class="submit-button" /&gt; &lt;/td&gt; &lt;/tr&gt; </code></pre> <p>}</p> <p>This is all fine and good. It renders the following output.</p> <pre><code> &lt;input name="[0].Name" type="hidden" value="" /&gt; .... </code></pre> <p>However, in my Controller, I can't seem to receive values back for the Model (binding).</p> <pre><code>[HttpPost] public ActionResult Approve(Approve approveModel) { .... approveModel has all default values } </code></pre> <p>Can someone shed light on what I am doing wrong here? I abbreviated the code, I am using the Editor Template with other EditorFor and HiddenFor fields from my model...</p> <p>Edited: I basically have a table layout, each with the User's Name, a textbox where I can enter their role (User or Admin) and then an Approve button which submits to my controller. Hence the reason I want to only return a single Approve object. I can return the entire IEnumerable to my Controller, but if I do that, how can I tell which of the items was the one I clicked the Approve button (submit) for?</p> <p>EDIT: So I have modified the code so that I have a single form surrounding my entire View Approve.cshtml</p> <pre><code>@model IEnumerable&lt;MvcWebsite.Models.Approve&gt; @using (Html.BeginForm("Approve", "Program", FormMethod.Post)) { &lt;table&gt; &lt;tr&gt; &lt;th&gt; Name &lt;/th&gt; &lt;/tr&gt; @Html.EditorForModel() &lt;/table&gt; } </code></pre> <p>And then changed the controller to </p> <pre><code>[HttpPost] public ActionResult Approve(IEnumerable&lt;Approve&gt; approvals) { // ??????????????????????? } </code></pre> <p>Now I'm still not clear on how to know which row I clicked Approve for. I know there are other ways to accomplish this task (create a checkbox for approve, and approve anything checked, etc.) However, I need the ability to click a button and only save 1 row back to the database, regardless if the user entered information into the other rows. Is it better practice to wrap my IEnumerable inside of it's own model (i.e. AllApprovals) and then add helper properties to that parent model (SelectedIndex, etc.)? If that is the approach to take, then how do I set the SelectedIndex after clicking an Approve button? Is that still jquery magic or is there a correct MVC way to accomplish this? Jquery magic seems very hackish to me?</p> <p>EDIT: Based on Brian's response, here is my final. Still doesn't feel quite right, but it works!</p> <p>View</p> <pre><code>@model IEnumerable&lt;MvcWebsite.Models.Approve&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt; Name &lt;/th&gt; &lt;/tr&gt; @Html.EditorForModel() &lt;/table&gt; </code></pre> <p>Editor Template</p> <pre><code>@using (Html.BeginForm("Approve", "Registration", FormMethod.Post)) { &lt;tr&gt; &lt;td&gt; @Html.HiddenFor(m =&gt; m.Name) @Html.EditorFor(m =&gt; m.Role) &lt;/td&gt; &lt;td&gt; &lt;input type="submit" value="Approve" class="submit-button" /&gt; &lt;/td&gt; &lt;/tr&gt; } </code></pre> <p>Controller</p> <pre><code>[HttpPost] public ActionResult Approve([Bind(Prefix="approval")]Approve approval) { // WORKS! } </code></pre>
    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.
 

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