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

Dictionary keys binding

1 Answer 893 Views
Grid
This is a migrated thread and some comments may be shown as answers.
shmuel
Top achievements
Rank 1
shmuel asked on 08 Jun 2015, 01:05 PM

i have a module with additional "dynamic" propertys, stored in a dictionary (loaded from DB, style of Entity-Attribute-Value

the module like this:

public class Contact
{
    public int ContactID { get; set; }
    public string FullName { get; set; }
    //...more fixed properties
 
    #region "handel dynamic property"
 
    public Dictionary<int, string> ValuesForDynamicProps = PropsNameToSpcailParameter.Values.ToDictionary(x => x.IdParameter, x => "");
 
    private void intDicFromContact(DAL.Contact contact)
    {
        foreach (var item in contact.ContactsParameters)
        {
            ValuesForDynamicProps[item.Parameter] = item.GetString;
        }
    }
     
    static StudentVM()
    {
        PropsNameToSpcailParameter = DAL.Contact.SpecialParamertersTypes().ToDictionary(x => x.ID, x =>
            new ModelSpecialParameter(x));
    }
 
    public static Dictionary<int, ModelSpecialParameter> PropsNameToSpcailParameter;
    #endregion
}

the View:

 

@(Html.Kendo().Grid(Contact)
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(e => e.FullName).Width(120).Title("Name");
        //...more
          
        foreach (var item in ShutafimOrganizationsWebApp.UI.Models.StudentVM.PropsNameToSpcailParameter)
        {
            columns.Bound(e => e.ValuesForDynamicProps[item.Key]).Title(item.Value.DisplayName);
        }

I receive an exeption ""Bound columns require a field or property access expression."

 

MY QUESTION: i serarch an elegant and easy way to show in grid an model with posibilty to add and remove "virtual" propertys.

 

Ps: i asked in stackoverflow: http://stackoverflow.com/questions/30650326.

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 10 Jun 2015, 12:14 PM
Hello,

As suggested by the exception, dictionary entries cannot be bound. You could show them using template columns e.g.

Server binding:
foreach (var item in ShutafimOrganizationsWebApp.UI.Models.StudentVM.PropsNameToSpcailParameter)
{
    columns.Template(o => o.ValuesForDynamicProps[item.Key]);
}
Ajax binding:
foreach (var item in ShutafimOrganizationsWebApp.UI.Models.StudentVM.PropsNameToSpcailParameter)
{
      columns.Template(o => { }).ClientTemplate(string.Format("#:ValuesForDynamicProps['{0}']#", item.Key));
}

Another option if they should be bound is to use approach similar to the one demonstrated for DataTables in this code-library project - set the grid model type to dynamic which will prevent the validation on the server and use the string overload of the Bound method to specify the field name:
@(Html.Kendo().Grid<dynamic>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound("FullName").Width(120).Title("Name");
        foreach (var item in ShutafimOrganizationsWebApp.UI.Models.StudentVM.PropsNameToSpcailParameter)
        {
            columns.Bound(string.Format("ValuesForDynamicProps['{0}']", item.Key)).Title(item.Value.DisplayName);
        }
    })
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("ReadAction", "Controller"))
    )
)


Regards,
Daniel
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
shmuel
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or