Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In ASP.NET MVC, if you have a model class like:</p> <pre><code>public class PageModel { public Collection&lt;RowItem&gt; RowItems { get; set; } } public class RowItem { public int Id {get;set;} public string MoreFields { get; set; } } </code></pre> <p>And your javascript adds rows like so: </p> <pre><code>&lt;script type="text/javascript"&gt; var currentRowIndex = 0; $(document).ready(function () { SetFocusEventForInputs('unSelected0'); }); function SetFocusEventForInputs(className) { var inputSelector = '.' + className; $(inputSelector).focusin(function () { AddNewRowTo(this); $(inputSelector).unbind('focusin').removeClass(className); }); } function AddNewRowTo(sendingInputField) { currentRowIndex++; var className = 'unSelected' + currentRowIndex; var collectionNamePrefix = 'RowItems[' + currentRowIndex + '].'; var idField = $('&lt;input/&gt;').attr('name', collectionNamePrefix + 'Id').attr('type', 'text').attr('class', className); var moreFields = $('&lt;input/&gt;').attr('name', collectionNamePrefix + 'MoreFields').attr('type', 'text').attr('class', className); var cell = $('&lt;td&gt;&lt;/td&gt;').append(idField).append(moreFields); var row = $('&lt;tr&gt;&lt;/tr&gt;').append(cell); $(sendingInputField).parents("tr").after(row); SetFocusEventForInputs(className); } &lt;/script&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt; &lt;input name="RowItems[0].Id" type="text" class="unSelected0" /&gt; &lt;input name="RowItems[0].MoreFields" type="text" class="unSelected0" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>The default model binder in MVC should resolve it just fine when it gets posted</p> <pre><code>[HttpPost] public ActionResult YourPostActionHere(PageModel model) { var count = model.RowItems.Count(); etc... } </code></pre>
 

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