Search code examples
teleriktelerik-gridtelerik-mvcmvc-editor-templates

How to pass model to editor templates


I am using Telerik MVC Grid, with ajax binding and I use grid editing in InCell editing mode with editor templates. I would like to pass model to the editors.

As I know if I had used the server binding, it could be possible to pass the model to the editor templates. But I am not sure about Ajax binding.

Is it possible to pass a model to editor templates when you use Ajax binding?


Solution

  • Yes you can! It does it automatically. just if your template editor is a list box , you should pass the list items through a ViewBag.XXX property. Here is an example of ProductSelector.ascx editor template :

        <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<int>" %>
    
    <%Html.Telerik().ComboBox()
            .Name(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))
            .AutoFill(true)
                .BindTo(((IEnumerable<Aien.CRM.Biz.Entities.Product>)ViewBag.PossibleProducts).Select(option => new SelectListItem
                {
                    Text = (option == null ? "(None)" : option.Title),
                    Value = option.Id.ToString()
                }))
            .OpenOnFocus(true)
            .Render();
    
    %>
    

    don't forget to put a UiHint attribute for the related model property.