MVC3 and using the Telerik Grid.
I’m using Phil Haacks Hidden Indexer Input For Model to bring form values back. Only problem is that when using Ajax as the databinder any Templates used need to have a corresponding ClientTemplate. This is where I’m having a problem. How do I insert this, and the iterator into the ClientTemplate, which is expecting a string?
This is what I’ve tried to no avail … when I run the page I get an error saying ‘Iter’ is not defined.
<% int Iter = 0; %>
<% Html.Telerik().Grid(Model.TransferStudents)
.Name("TransferStudents")
.Columns(columns =>
{
columns.Bound(o => o.Name)
.Width(250);
columns.Template(o =>{%>
<%: Html.HiddenFor(model => Model.TransferStudents[Iter].StudentId)%>
<%: Html.CheckBoxFor(model => Model.TransferStudents[Iter].Transfer)%>
<%})
.ClientTemplate("<input type='hidden' id='StudentId' value='<#= StudentId #>' /><input type='checkbox' name='Transfer' <#= Transfer? \"checked='checked'\" :\"\" #> />")
.HtmlAttributes(new { @style = "text-align: center;" })
.HeaderHtmlAttributes(new { @style = "text-align: center;" })
.HeaderTemplate("Check All <input type='checkbox' id='chkAll' />")
.Width(105);
columns.Template(o =>{ %><% Iter = Iter + 1; %><%})
.ClientTemplate("<# Iter = Iter + 1; #>")
.Hidden();
columns.Template(o =>{ %><%: Html.HiddenIndexerInputForModel()%><% })
.ClientTemplate("<#= Html.HiddenIndexerInputForModel() #>")
.Hidden();
})
.DataBinding(dataBinding => dataBinding.Ajax().OperationMode(GridOperationMode.Server).Select("_Transfer", "Administration"))
.Pageable(paging => paging.Style(pagerStyles).PageSize(2, new[] { 5, 10, 15, 25, 50, 200 }).Position(GridPagerPosition.Top).Total((int)ViewData["total"]))
.Sortable()
.NoRecordsTemplate("<p class='instructions'>No Records Available</p>")
.Render();
%>
Any pointers would be appreciated.
The ClientTemplate expects a string which will be translated to JavaScript and executed in the browser (client-side). Any server code such as Html.HiddenIndexerInputForModel() won’t work – there is no JavaScript equivalent. You need to use plain html as you have done in the other column.