1

I want populate a TokenBox from a database using the property tokenBoxSettings.Properties.DataSource

TokenBoxView.cshtml

 groupSettings.Items.Add(
                        formLayoutSettings.Items.Add(i =>
                        {
                            i.FieldName = "email";
                            i.Caption = "Email";
                            i.NestedExtensionType = FormLayoutNestedExtensionItemType.TokenBox;
                            TokenBoxSettings tokenBoxSettings = (TokenBoxSettings) i.NestedExtensionSettings;
                            tokenBoxSettings.Width = 350;
//data binding
        tokenBoxSettings.Properties.DataSource = mainController.GetMails();
                            tokenBoxSettings.Properties.TextField = "email_empresarial";
                            tokenBoxSettings.Properties.ValueField = "email_empresarial";
                            tokenBoxSettings.Properties.IncrementalFilteringMode = IncrementalFilteringMode.Contains;
                            tokenBoxSettings.Properties.ValueSeparator = ';';
                        })
                        );

TokenBoxController.cs

//mainController
//I created a dictionary based on the result of select
public Dictionary<string, string> GetMails()
        {
            var email = db.usuario.ToList().Select(e => new { e.email_empresarial });
            var emails = new Dictionary<string, string>();

            foreach (var mail in email)
            {
                correos.Add(mail.ToString(), mail.ToString());
            }
            return emails;
}

But it shows me the "object explicitly", I only need the value, for example kenneth or manuel

tokenBox list

What am I doing wrong? or with what other approach I can do?

bryannsi
  • 97
  • 10

1 Answers1

1

You are specifying same email_empresarial field name for both tokenBoxSettings.Properties.TextField and tokenBoxSettings.Properties.ValueField.

Since you are binding your TokenBox to Dictionary, try changing settings for TextField and ValueField to reference Dictionary Key and Value, like this:

tokenBoxSettings.Properties.TextField = "Value";
tokenBoxSettings.Properties.ValueField = "Key";

Also, in your GetMail() method you have declared the var emails but in the loop you are adding items to the undeclared correos variable. Are you sure you don't have a bug here?

Another note, in the Dictionary returned by GetMails() you populate both dictionary keys and values with the same value of mail.ToString(). Are you sure you really need to use Dictionary to bind your TokenBox? If keys and values are equal you may try going with plain List<string>.

andrews
  • 2,173
  • 2
  • 16
  • 29