Note that there are some explanatory texts on larger screens.

plurals
  1. POOpposite of "Model Binding To A List"
    primarykey
    data
    text
    <p>I would like to know people's thoughts on the best way to do the opposite to <a href="http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx" rel="nofollow noreferrer">Phil Haack's Model Binding To A List</a>. I have a form which contains several individual fields as well as form fields which are created dynamically using JQuery. This means that when I query the database for something to edit, it will return an object which in itself will have n number of objects (each the same) attached to it.</p> <p>I would like to know what people would suggest as the best way to create the "dynamic" parts of the forms and, in particular, ensure that the correct items are selected for the drop down lists. I have a vague idea that it will involve View User Controls but I am struggling with how to put it all together.</p> <p>The form:</p> <pre><code>&lt;form action="/MyItems/Edit" method="post"&gt; Title: &lt;input type="text" name="Title" value="" /&gt;&lt;br /&gt; Description &lt;input type="text" name="Description" value="" /&gt;&lt;br /&gt;&lt;br /&gt; &lt;input type="hidden" name="myItem.Index" value="0" /&gt; &lt;input id="item[0].Amount" name="item[0].Amount" type="text" value="" /&gt; &lt;select id="item[0].selectA"&gt;&lt;option value="1"&gt;1&lt;/option&gt; &lt;option value="2"&gt;2&lt;/option&gt;&lt;option value="3"&gt;3&lt;/option&gt;&lt;/select&gt; &lt;select id="item[0].selectB"&gt;&lt;option value="1"&gt;1&lt;/option&gt; &lt;option value="2"&gt;2&lt;/option&gt;&lt;option value="3"&gt;3&lt;/option&gt;&lt;/select&gt; &lt;input type="hidden" name="myItem.Index" value="1" /&gt; &lt;input id="item[1].Amount" name="item[1].Amount" type="text" value="" /&gt; &lt;select id="item[1].selectA"&gt;&lt;option value="1"&gt;1&lt;/option&gt; &lt;option value="2"&gt;2&lt;/option&gt;&lt;option value="3"&gt;3&lt;/option&gt;&lt;/select&gt; &lt;select id="item[1].selectB"&gt;&lt;option value="1"&gt;1&lt;/option&gt; &lt;option value="2"&gt;2&lt;/option&gt;&lt;option value="3"&gt;3&lt;/option&gt;&lt;/select&gt; &lt;input type="hidden" name="myItem.Index" value="2" /&gt; &lt;input id="item[2].Amount" name="item[2].Amount" type="text" value="" /&gt; &lt;select id="item[2].selectA"&gt;&lt;option value="1"&gt;1&lt;/option&gt; &lt;option value="2"&gt;2&lt;/option&gt;&lt;option value="3"&gt;3&lt;/option&gt;&lt;/select&gt; &lt;select id="item[2].selectB"&gt;&lt;option value="1"&gt;1&lt;/option&gt; &lt;option value="2"&gt;2&lt;/option&gt;&lt;option value="3"&gt;3&lt;/option&gt;&lt;/select&gt; &lt;/form&gt; </code></pre> <p>Everything from the hidden input to the end of the second select list could be repeated n times. The DTO that is being hydrated from the database query looks like the following:</p> <pre><code>public class MyItem { public string Name { get; set; } public string Description { get; set; } public IEnumerable&lt;SelectItem&gt; SelectItems { get; set; } } </code></pre> <p>Each <code>SelectItem</code> then looks like:</p> <pre><code>public class SelectItem { public int SelectA { get; set; } public int SelectA { get; set; } } </code></pre> <p>I hope I have explained this ok, thanks in advance for anyone taking the time to have a look at this.</p>
    singulars
    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.
    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