Dictionary keys binding

2 posts, 0 answers
  1. shmuel
    shmuel avatar
    1 posts
    Member since:
    Sep 2011

    Posted 08 Jun 2015 Link to this post

    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.

  2. Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 10 Jun 2015 Link to this post

    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
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top