在ASP.NET MVC,如果你有一個模型類,如:
public class PageModel
{
public Collection<RowItem> RowItems { get; set; }
}
public class RowItem
{
public int Id {get;set;}
public string MoreFields { get; set; }
}
而JavaScript添加行,像這樣:
<script type="text/javascript">
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 = $('<input/>').attr('name', collectionNamePrefix + 'Id').attr('type', 'text').attr('class', className);
var moreFields = $('<input/>').attr('name', collectionNamePrefix + 'MoreFields').attr('type', 'text').attr('class', className);
var cell = $('<td></td>').append(idField).append(moreFields);
var row = $('<tr></tr>').append(cell);
$(sendingInputField).parents("tr").after(row);
SetFocusEventForInputs(className);
}
</script>
<table>
<tr>
<td>
<input name="RowItems[0].Id" type="text" class="unSelected0" />
<input name="RowItems[0].MoreFields" type="text" class="unSelected0" />
</td>
</tr>
</table>
在MVC的默認模式粘結劑應解決它就好了,當它張貼
[HttpPost]
public ActionResult YourPostActionHere(PageModel model)
{
var count = model.RowItems.Count();
etc...
}
謝謝,這真是太棒了! – 2011-06-03 09:58:13