Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I bind nested ViewModels from View to Controller in MVC3?
    primarykey
    data
    text
    <p>I am developing an ASP.NET MVC 3 application in C# and I use Razor. I am now dealing with a problem concerning the binding of objects through ViewModels passed/received to/from the View by the Controller. Let's make it clear. I have the following ViewModels:</p> <pre><code>public class ContainerViewModel { public int ContainerId {get; set;} public string ContainerName {get; set;} public List&lt;ItemPostModel&gt; ItemData {get; set;} } public class ItemPostModel { public int ItemId {get; set;} public string ItemName {get; set;} public int ItemValue {get; set;} } </code></pre> <p>The <strong>ContainerViewModel</strong> is used to pass the data to the View. Its properties <strong>ContainerId</strong> and <strong>ContainerName</strong> are used just for display purposes. The <strong><code>List&lt;ItemPostModel&gt;</code></strong> property has to be filled using a <strong>Form</strong>. The View looks something like this (it is a simplified version):</p> <pre><code>&lt;strong&gt;@Model.ContainerName&lt;/strong&gt; @using (Html.BeginForm()) { &lt;fieldset&gt; @foreach(var item in Model.ItemData) { @Html.TextBox(item.ItemId); @Html.TextBox(item.ItemName); @Html.TextBox(item.ItemValue); &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; } &lt;/fieldset&gt; } </code></pre> <p>The <strong>Controller</strong> corresponding <strong>action methods</strong> are as follows:</p> <pre><code>public ActionResult UpdateItems() { //fill in the ContainerViewModel lcontainer return View("UpdateItems", lcontainer); } [HttpPost] public ActionResult UpdateItems(int containerId, ItemPostModel itemData) { //store itemData into repository } </code></pre> <p>The problem is that with this code the <strong>ItemPostModel itemData</strong> passed to the <strong>Post ActionMethod UpdateItems</strong> is always empty. The <strong>containerId</strong> is correctly passed. Same result if I use the following code in the Controller (obviously not DRY);</p> <pre><code>[HttpPost] public ActionResult UpdateItems(ContainerViewModel container) { //extract itemData from ContainerViewModel container //store itemData into repository } </code></pre> <p>How can I <em>"teach"</em> the application that I want the form elements stored in the <strong><code>List&lt;ItemPostModel&gt;</code></strong>? Shall I modify the <strong>ModelBinder</strong> or there is a simpler way to perform this task? Thanks everybody for your answers.</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.
 

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