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

Editor Template Cached?

0 Answers 67 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Hardy Wang
Top achievements
Rank 1
Hardy Wang asked on 03 Oct 2012, 12:33 AM
I am using 2012.2.607.

My model
public class MyViewModel {
    public List<ExtendedPropertyViewModel> ExtendedProperties {
        get;
        set;
    }
}
 
public class ExtendedPropertyViewModel {
    public int? Id {
        get;
        set;
    }
 
    public string Name {
        get;
        set;
    }
 
    [UIHint("EnumDropDownEditor")]
    public virtual DataType  DataType {
        get;
        set;
    }
 
    public bool? Required {
        get;
        set;
    }
}
 
public enum DataType {
    Integer = 1,
    String = 2,
    Decimal = 3,
    DateTime = 4
}

My editor templator is like below. I try to load all values in Enum to a dropdown list.
@model dynamic
 
@using System.ComponentModel.DataAnnotations
@using Telerik.Web.Mvc.UI
 
@{
    // This will be used later to decide if we display a dropdown or textbox
    SelectList selectList = null;
 
    // This code creates a select list of non-nullable enums
    if (ViewData.ModelMetadata.ModelType.IsEnum) {
        selectList = new SelectList(ViewData.ModelMetadata.ModelType.GetEnumNames(), Model.ToString());
    }
 
    // This code creates a select list for nullable enums
    if (ViewData.ModelMetadata.IsNullableValueType && Nullable.GetUnderlyingType(ViewData.ModelMetadata.ModelType).IsEnum) {
        List<string> names = new List<string>();
        names.Add("");
        names.AddRange(Nullable.GetUnderlyingType(ViewData.ModelMetadata.ModelType).GetEnumNames());
        selectList = new SelectList(names, Model == null ? "" : Model.ToString());
    }
 
    // Display as a dropdown list if there is a select list, otherwise display as a textbox
    if (selectList != null) {
        string propertyName = ViewData.ModelMetadata.PropertyName;
    @(Html.Telerik().DropDownList().Name(propertyName).BindTo(selectList))
    } else {
    @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue)
    }
 
    if (Request.IsAjaxRequest()) {
        ViewData.ModelState.Clear();
    }
}

And my view is like below to load model MyViewModel, the list in it is set to Grid's model.
@(Html.Telerik().Grid(Model.ExtendedProperties)
.Name("ExtendedPropertiesGrid")
.DataKeys(keys => keys.Add(c => c.Id))
.ToolBar(commands => commands.Insert().ButtonType(GridButtonType.ImageAndText).ImageHtmlAttributes(new { style = "margin-left:0" }))
.DataBinding(dataBinding => dataBinding.Ajax()
    .Select("SelectAction", "MyController")
    .Insert("InsertAction", "MyController")
    .Update("SaveAction", "MyController")
    .Delete("DeleteAction", "MyController")
)
.Columns(columns =>
{
    columns.Bound(p => p.Name).Title("Name");
    columns.Bound(p => p.Required).Title("Is Required").ClientTemplate("<#= Required ? '<img src=\"/Content/images/iconBlueCheck.gif\"/>' : '' #>");
    columns.Bound(p => p.DataType).Title("Data Type").ClientTemplate("<#=DataTypeValues[DataType]#>");
    columns.Command(commands =>
    {
        commands.Edit().ButtonType(GridButtonType.ImageAndText);
        commands.Delete().ButtonType(GridButtonType.ImageAndText);
    }).Width(150).Title("");
})
.Editable(editing => editing.Mode(GridEditMode.InLine))
)

In the grid I can insert new record, edit existing one. I set break point in my editor templator and I noticed itwas called only one time when view is loaded initially.

When I click insert button it works fine, dropdown box is displayed. When I click on edit button, the selected row also turns to textboxes and dropdown list, but the break point in the editor template is never hit again. So the problem becoems that dropdown list's content is always the initial data without current value as selected item.

Is there a way I can force the Grid to reload editor templator everytime edit button is clickec?





Tags
General Discussions
Asked by
Hardy Wang
Top achievements
Rank 1
Share this question
or