我遇到了同样的问题,我已经找到了解决方案。不要认为它是最漂亮的,但它对我来说很理想,因为我的要求之一是能够使用 jQuery 的Jeditable 插件编辑表格数据。
所以我使用 MVCContrib 的 Grid 扩展生成了一个表:
Html.Grid<Somenamespace.Line>( Model.InvoiceLines )
.Attributes( id => "InvoiceGrid" )
.Columns( column => {
column.For( li => li.LineItem.ItemDescription ).Attributes( name => ".LineItem.ItemDescription", @class => "click" );
column.For( li => li.LineItem.InvoiceUnitNetPrice ).Named( "Unit net price " ).Attributes( name => ".LineItem.InvoiceUnitNetPrice", @class => "click" );
column.For( li => li.LineItem.InvoiceQuantity ).Attributes( name => ".LineItem.InvoiceQuantity", @class => "click" );
})
.Render();
//rest of the code
Html.Submit("_submit", "Save");
现在你可以就地编辑值,但它不会升级相应的模型。
用户点击提交按钮后,所有的魔法都会发生:
$(document).ready(function() {
$('#_submit').click(function(e) {
e.preventDefault();
$('#InvoiceGrid tbody tr').each(function(index) {
var hidden = $('<input />').attr({ type: 'hidden', name: 'InvoiceLines.Index', value: index });
$(this).children('td:first-child').before(hidden);
$(this).children('td:not(:first-child)').each(function() {
$(this).append($('<input />').attr({ type: 'hidden', value: $(this).text(), name: 'InvoiceLines[' + index + ']' + $(this).attr('name') }));
});
});
$('form').submit();
});
//editable stuff
$('.click').editable(function(value, settings) {
return (value);
}, { submit: 'OK' });
});
在每个 TD 中,我使用来自该 TD 的值创建隐藏输入,在带有索引的每一行输入中,这里最重要的是“名称”属性:模型中集合的名称[这里是索引].rest .of.path,所以在这种特殊情况下(示例):
InvoiceLines[2].LineItem.ItemDescription
希望它会有所帮助,因为丰富的网格并不总是一个答案;)
问候
马特乌斯