How to edit tabular data (ASP MVC)

后端 未结 4 732
情歌与酒
情歌与酒 2021-02-05 21:27

I need to be able to edit a table of data in the browser.

I have seen in MVCContrib there is a HTML helper to render out a table. Useful... but what about if I want the

4条回答
  •  悲&欢浪女
    2021-02-05 21:53

    I've got the same problem, and I have found a solution for it. Don't think it's the most beautiful, but it's ideal for me, because one of my requirements was be able to edit table data using jQuery's Jeditable plugin.

    So I generate a table using MVCContrib's Grid<> extension:

    Html.Grid( 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");
    

    Right now You can edit in place values, but it doesn't upgrade corresponding model. All the magic happens after user clicks submit button:

    $(document).ready(function() {
            $('#_submit').click(function(e) {
                    e.preventDefault();
                    $('#InvoiceGrid tbody tr').each(function(index) {
                        var hidden = $('').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($('').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' });
            });
    

    In every TD I create hidden input, with value from that TD, in every row input with Index, and the most important here is 'name' attribute: Name of collection in Model[here goes index].rest.of.path, so in this particular case (example):

    InvoiceLines[2].LineItem.ItemDescription
    

    Hope it'll help, because rich grid isn't always an answer ;)

    Regards Mateusz

提交回复
热议问题