This is a migrated thread and some comments may be shown as answers.

[Solved] MVC Grid Lost its HTML attributes when it's Used With Ajax

1 Answer 75 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Saadat
Top achievements
Rank 1
Saadat asked on 25 Jan 2012, 04:41 PM

I am using Mvc telerik Grid and using Ajax with paging.  (.RowAction) I set the value of its id through HTML Attributes. My problem is when i press [Page 2] Link on the buttom of the Grid, it goes back to controller and brings back new new for that page but somehow i lost HTML ID attribute in this process. If i see the source code of my page the <tr> does not have any id.

This works fine if i dont use Ajax.

Please Help me. I am pasting my View's Source code below.


@{Html.Telerik().Grid(Model)

.Name(

"Grid")

 .Pageable(paging =>paging.PageSize(10))

 

 .Columns(columns =>

 {

 columns.Bound(o => o.Name);

 columns.Bound(o => o.CarrierId);

 })

.Pageable(paging => paging.PageSize(10).Style(

 GridPagerStyles.NextPreviousAndNumeric).Position(GridPagerPosition.Bottom))

 .DataBinding(dataBinding => dataBinding.Ajax().Select(

 

"_Index", "Home"))

 .CellAction(cell =>

 

 {

if (cell.Column.Member == "Name"){

 

 cell.HtmlAttributes[

"id"] = cell.DataItem.CarrierId;

 }

 

})

.RowAction(row=>

{

 row.HtmlAttributes[

"id"] = row.DataItem.CarrierId;

}

 

)

.Render();

}

1 Answer, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 26 Jan 2012, 09:21 AM
Hello Saadat,

Basically when using Ajax binding for the Grid you need to handle the OnRowDataBound client event and execute your logic.
e.g.
C#: 
Html.Telerik().Grid(Model)
     .Name("Grid")
//...
    .ClientEvents(events=>events.OnRowDataBound("onRowDataBound"))
JavaScript: 
function onRowDataBound(e){
    $(e.row).attr('id',e.dataItem.CarrierId);
}
Also instead of the CellAction method you can set a client template instead
e.g. 
columns.Bound(o => o.Name).ClientTemplate("<div id='cell<#= CarrierId#>'></div>");

Please notice I use cell as prefix to the ID attribute to avoid duplicate IDs on the same page (rows would have the same ID as the cells).
I hope this helps. 


Kind regards,
Petur Subev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
Tags
Grid
Asked by
Saadat
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Share this question
or