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

[Solved] Grid column bind to object property

1 Answer 199 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.
Sandy
Top achievements
Rank 1
Sandy asked on 01 Dec 2010, 11:14 AM
Hi there,

I have this view model:

    public class ConfigViewModel
    {
        [ScaffoldColumn(true)]
        [UIHint("ReadOnlyString")]
        public string DataTypeFullName { get; set; }

        [ScaffoldColumn(true)]
        [UIHint("ReadOnlyString")]
        public string PropertyName { get; set; }

        [Display(Description = "Category")]
        [UIHint("ReadOnlyString")]
        public string LogicalAreaTypeName { get; set; }

        [UIHint("ReadOnlyString")]
        public string DisplayName { get; set; }

        public object CurrentValue { get; set; }
    }

And using ajax binding to grid like this:

       <% Html.Telerik().Grid<JService.Services.Configuration.ConfigViewModel>()
        .Name("ConfigurationsGrid")
        .DataBinding(dataBinding => dataBinding
            .Ajax()
            .Select("ConfigurationGridIndex", "Configuration", new { area = "System", format = "json" })
            .Update("ConfigurationGridUpdate", "Configuration", new { area = "System", format = "json" })
        )
        .DataKeys(keys =>
            keys.Add(u => u.PropertyName))
        .Columns(columns =>
        {
            columns.Bound(u => u.LogicalAreaTypeName).Title("Category").ReadOnly();
            columns.Bound(u => u.DisplayName).Title("Item").ReadOnly();
            columns.Bound(u => u.CurrentValue).Title("Current Value").Sortable(false);
            columns.Command(commands =>
            {
                commands.Edit();
            }).Title("Commands");
        })
        .Pageable(pager => pager.PageSize(10))
        .Sortable(s=> s.SortMode(GridSortMode.SingleColumn))
        .Groupable()
        .Filterable()
        .Render();
        %>

The problem I have is when in inline edit mode, the current value column is empty. No edit controls at all. So I thought I should add an object.ascx to EditorTemplates like below:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<object>" %>
<%  if(Model == null) { %>
    hi there
<%} else if(Model.GetType() == typeof(string)) { %>
    <%: Html.EditorFor(e => e.ToString()) %>
<%} else if(Model.GetType() == typeof(int)) { %>
    <%: Html.EditorFor(e => (int)e) %>
<%} else if(Model.GetType() == typeof(DateTime)) { %>
    <%: Html.EditorFor(e => (DateTime)e) %>
<%} else if(Model.GetType() == typeof(bool)) { %>
    <%: Html.EditorFor(e => (bool)e) %>
<%} else { %>
    <%: Html.EditorFor(e => e.ToString()) %>
<% } %>

But the Model is always null, and so displaying the debug message "hi there". Any ideas on how I can get this edit mode working for an object datatype?

Cheers,
Sandy.

1 Answer, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 01 Dec 2010, 12:56 PM
Hi Sandy,

 Any server side logic added in the editor template is executed only once when your grid is configured for ajax editing. An empty model object is created using Activator.CreateInstance and all reference properties are thus set to null. This is the reason why Model is always null - the template is rendered against the dummy object. Have in mind that during ajax binding the editor template is populated on the  cilent-side.

You need to use server side binding and editing if you need to execute server code in the editor template. 

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Sandy
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Share this question
or