How to assign values at runtime

I am able to populate the multi-select dropdown and select items. I can then pull those items and save them to a database.  However, I can't see a way to mark entries as checked.  Here is my markup and code:

 <SfMultiSelect ID="SelectAttendees"
                                   @bind-Value="@MultiVal"
                                   TValue="string[]"
                                   TItem="SelectionList"
                                   Mode="@VisualMode.CheckBox"
                                   Placeholder="Attendees"
                                   DataSource="@Attendees">
                        <MultiSelectFieldSettings Text="Text" Value="ID"></MultiSelectFieldSettings>
                    </SfMultiSelect>

@code {
    public string[] MultiVal = new string[10];
    private List<SelectionList> Attendees = new List<SelectionList>();

 public class SelectionList
    {
        public string ID { get; set; }
        public string Text { get; set; }
    }
}

Note: it would be nice to be able to bind this control to my child table so that entries get loaded and saved from/to the List<T> object automatically.

TIA,

Miles




5 Replies

PM Ponmani Murugaiyan Syncfusion Team March 29, 2021 07:13 AM UTC

Hi Miles, 

Greetings from Syncfusion support. 

Query: How to assign values at runtime. 
 
You can dynamically update value for Multiselect component using bind-Value property. Here we demonstrated with an example of dynamically updating value through button click.  

 
<SfMultiSelect ID="SelectAttendees" Placeholder="e.g. Australia" @bind-Value="@MultiVal" Mode="@VisualMode.CheckBox" DataSource="@Country"> 
    <MultiSelectFieldSettings Value="Code" Text="Name"></MultiSelectFieldSettings> 
</SfMultiSelect> 
 
<button @onclick="@UpdateValue">Dynamically update value</button> 
 
@code { 
 
    public string[] MultiVal { get; set; } 
 
    public void UpdateValue() 
    { 
        MultiVal = new string[] { "BM", "CA" }; 
    } 
 
    public class Countries 
    { 
        public string Name { get; set; } 
 
        public string Code { get; set; } 
    } 
 
    List<Countries> Country = new List<Countries> 
    { 
        new Countries() { Name = "Australia", Code = "AU" }, 
        new Countries() { Name = "Bermuda", Code = "BM" }, 
        new Countries() { Name = "Canada", Code = "CA" }, 
        new Countries() { Name = "Cameroon", Code = "CM" }, 
    }; 
} 
 
 
Please get back us if you need further assistance. 

Regards, 
Ponmani M 



MG Miles Gibson April 4, 2021 11:30 PM UTC

Ok, I got this to work... EXCEPT.  It only works with an array of string.  If you try to use an array of <T> where T is a simple class, then it always returns null when you click a button etc.  So although now I can select and retrieve a list of initials, I have to do an additional lookup in the database to retrieve the Id's for those initials.

And with this construct, assigning a list of values at runtime in either OnParameterSet or OnInitialize events doesn't seem to assign it.  Do I need to do this in the PreRender event perhaps?

TIA,

Miles




PM Ponmani Murugaiyan Syncfusion Team April 5, 2021 02:36 PM UTC

Hi Miles, 

Thanks for the update. 

We have validated your query and we have considered it as a bug and logged the defect report  in our end and including the defect fix in our upcoming patch release which is expected to be rolled out on or before mid of April, 2021.  Until then we appreciate your patience.  
    
You can now track the current status of your request through this link.        
  

Regards, 
Ponmani M 



IS issam replied to Miles Gibson April 23, 2021 10:24 PM UTC

Ok, I got this to work... EXCEPT.  It only works with an array of string.  If you try to use an array of <T> where T is a simple class, then it always returns null when you click a button etc.  So although now I can select and retrieve a list of initials, I have to do an additional lookup in the database to retrieve the Id's for those initials.

And with this construct, assigning a list of values at runtime in either OnParameterSet or OnInitialize events doesn't seem to assign it.  Do I need to do this in the PreRender event perhaps?

TIA,

Miles



Can you share your workaround please ?

i am facing the same problem .

thanks


MG Miles Gibson April 23, 2021 10:55 PM UTC

Here you go:

<div class="control-styles">
                        <SfMultiSelect ID="SelectAttendees"
                                       @bind-Value="@MultiVal"
                                       TValue="string[]"
                                       TItem="SelectionListItem"
                                       Mode="@VisualMode.CheckBox"
                                       Placeholder="Select Attendees"
                                       DataSource="@Attendees">
                            <MultiSelectFieldSettings Text="Text" Value="ID"></MultiSelectFieldSettings>
                        </SfMultiSelect>
</div>
@code {
  public string[] MultiVal { get; set; }
  private List<SelectionListItem> Attendees = new List<SelectionListItem>();
 protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        AssignExistingAttendees();
    }
  public void AssignExistingAttendees()
    {
          // source data is a comma delimited string stored in the database
          MultiVal = calendaritem.AttendeesList.Split(',');
    }
public class SelectionListItem
    {
        public string ID { get; set; }
        public string Text { get; set; }
    }
}


Loader.
Up arrow icon