Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 WebGrid: Can htmlAttributes be used on rows/columns?

I'm using a WebGrid to create a paged/sortable list in MVC3. I've created an AJAX enabled Delete button which makes the delete call via AJAX, after which I'd like it to remove the row from the table.

The way I'd like to achieve this is by having an id or data-id attribute on the <tr> in the table so that I can easily manipulate it using jQuery. However I can't work out how to add attributes to rows when using a WebGrid.

I know that attributes can easily set at the grid level like so:
@grid.GetHtml(htmlAttributes: new { id = "gridMapping", style = "width:100%;" },

However I don't know how to achieve the same at the row/column level.

like image 333
Peter Bridger Avatar asked Nov 13 '22 12:11

Peter Bridger


1 Answers

@grid.GetHtml(
    columns: grid.Columns(
        grid.Column(format: (item) => Html.CheckBox("SelectedInvoice",new { value=item.transactionId})),
   //// rest of your columns here
     )
 )

so one way would be putting an HTML helper method in place that can handle your htmlAttributes. Other way - using combination of format: and Html.Raw And the last, but may be the easiest: javascript (jQuery)

so you can try something like :

 $('#grid tr').each(function(){
     $(this).attr('yourhtmlattribute','value');
 });

and in similar way for TDs.

like image 114
Paweł Staniec Avatar answered Dec 26 '22 01:12

Paweł Staniec