Setting drop down items as "selected" from C# code

I have a Blazor SfMultiSelect inside a custom control I created. I want to pass to my custom control a list of items which should be rendered as "checked" in the SfMultiSelect when my custom control renders.

So, in my custom control I exposed a property to receive the list of items which should be "checked".

Inside my custom control, my SfMultiSelect was coded as follows:

<SfMultiSelect @ref="DDLDocumentos" ID="ddlDocumentos" TValue="List<TipoDocumentoDistribuicaoVM>" TItem="TipoDocumentoDistribuicaoVM" AllowFiltering="true" Placeholder="Selecione os Documentos..." Mode="@VisualMode.CheckBox"
                               DataSource="@LstTiposDocumento" @bind-Value="@TiposDocumentoSelecionados" ShowSelectAll="true" EnableSelectionOrder="false" ShowDropDownIcon="true" FilterBarPlaceholder="Pesquise..." PopupHeight="350px">
                    <MultiSelectFieldSettings Value="TipoDocumento" Text="Nome"></MultiSelectFieldSettings>
                    <MultiSelectEvents TItem="TipoDocumentoDistribuicaoVM" TValue="List<TipoDocumentoDistribuicaoVM>" ValueChange="@TiposDeDocumentosSelecionadosValueChangeHandler"></MultiSelectEvents>
                </SfMultiSelect>


The bind-Value="@TiposDocumentoSelecionados" points to a variable which I exposed as parameter, like this

[Parameter]
public List TiposDocumentoSelecionados { get; set; }


So, in pages where I need my custom control, I can pass in the list of items I want to be rendered as "checked", so in those pages I do the follow:

 <MyCustomComponent @ref="cmpSelecaoDocumentoAssunto" TiposDocumentoSelecionadosChanged="@TipoSelecionadosNoComponente" AssuntosSelecionadosChanged="DocsSelecionadosNoComponente" TiposDocumentoSelecionados="objRegraDistribuicaoVM.TiposDeDocumento"></MyCustomComponent>


At TiposDocumentoSelecionados="objRegraDistribuicaoVM.TiposDeDocumento" I am passing the list os items which should be rendered as"checked", but, none of the items of the SfMultiSelect appears checked.

How can I make this work?


OBS: I tried to find some method of the SfMultiSelect which select items by code but I only find the

cmpSelecaoDocumentoAssunto.SelectAllAsync(true) which works, but i need a method to select specific items,not all of them.



10 Replies

BC Berly Christopher Syncfusion Team December 23, 2021 12:03 PM UTC

Hi Ewerton Luis de Mattos, 
  
Greetings from Syncfusion support. 
  
We don’t have option to select particular item in the MultiSelect component. So, we can assign the required values with help of @bind-value attribute. For your reference, we have prepared the sample and attached it below. 
  
  
Regards, 
Berly B.C 



EL Ewerton Luis de Mattos December 23, 2021 10:39 PM UTC

Thanks for your answer, the example you provided works as expected, but my code not. So I spend all my day investigating it and I found the problem, which I believe it is a bug in the SfMultiSelect.

I edited and documented your example do demonstrate the issue, anyway let me describe it here.

The problem is: 

  • My model have an Id property. When I pass to my component a list of items which should be rendered as checked in the SfMultiSelect, the Id property is greater than zero because it was read from the database.
  • The list off all items in the SfMultiSelect (the datasource of the SfMultiSelect) have all of its Id properties equal zero, thats becouse this datasource was generated from a Enum.
  • In this case, the SfMultiSelect will not render the itens passed as parameter as checked.


To make it work, both the itens in the Datasource and in the @bind-Value should have the exact same VALUES! Which is very unlikely to happen.

In my case, the issue happens because my model have this Id property:

public class Countries
{
    public int Id { get; set; } // Maybe a property to represent the PK saved in the database, anyway I dont use it in the SfMultiSelect
    public string Name { get; set; } // The Text of the SfMultiSelect
    public string Code { get; set; } // The Value of the SfMultiSelect
}

Probably the Id property will be greater than ZERO when these entities are read from the database.

In the component the Id property of the datasource list will be ZERO because it was generated from an Enum I have.

I hope it make clear the issue and I hope you guys agree that it seems to be a bug.

Please, let me know if you will treat it as bug.

Also, let me know if you have some suggestions on how to workaround 



Attachment: Blazor_Server_Ewerton_dee7b8fb.zip


BC Berly Christopher Syncfusion Team December 27, 2021 03:58 PM UTC

Hi Ewerton, 
  
We will check and update the details in two business days (29th December 2021).  
  
Regards, 
Berly B.C 



EL Ewerton Luis de Mattos replied to Berly Christopher December 29, 2021 06:27 PM UTC

Ok. I understand the details you provided above but please, do not mix the issues. In fact, the details you provided above will only confuse future readers of this post. I suggest to delete it.

The example I provided attached exemplifies exactly what is the problem. The SfMultiSelect should consider ONLY the property used as Value in the MultiSelectFieldSettings to determine if an item should be rendered as checked or not.

In the example below, ONLY the property Code, should be considered.

<MultiSelectFieldSettings Text="Name" Value="Code"></MultiSelectFieldSettings>

Any other property (including the Id property) has nothing to do in this case.






BC Berly Christopher Syncfusion Team December 30, 2021 03:35 PM UTC

Hi Ewerton, 
  
Thanks for sharing the information to us.  
  
Query 1: 
  
The example I provided attached exemplifies exactly what is the problem. The SfMultiSelect should consider ONLY the property used as Value in the MultiSelectFieldSettings to determine if an item should be rendered as checked or not. 
  
Response: 
  
We were able to reproduce the reported issue. We will validate and update the further details on tomorrow (31st December 2021). 
  
Regards, 
Berly B.C 



BC Berly Christopher Syncfusion Team December 31, 2021 09:01 AM UTC

Hi Ewerton, 
  
We need two more days to validate the issue at our end. So, we will provide the further details on 4th January 2022. We appreciate your patience until then. 
  
Regards, 
Berly B.C 



BC Berly Christopher Syncfusion Team January 3, 2022 12:06 PM UTC

Hi Ewerton, 
  
When using TValue as complex object, we need to define the properties same for both data source and @bind-Value class. As per the C# standard behavior, the class with different properties will return false when comparing the each class object. So, we suggest you to define the properties same for both class to get rid of the reported issue.  
  
Regards, 
Berly B.C 
 



EL Ewerton Luis de Mattos replied to Berly Christopher January 3, 2022 04:24 PM UTC

I already did this to workaround this problem. But this absolutely does not make sense, the right way to compare is looking ONLY to the property used as  Value="Code" in the MultiSelectFieldSettings  

Furthermore, it is common in projects to use ViewModels, those view model, will never be 100% equal to the underlying complex type (Model).

I understand that the way C# compare objects, so, if I override the object Equals() in my ComplexType, the SfMultiSelect will work as the expected? I don't think so. Can you test it?

The problem I reported will be treated as bug or it will be working as is?




BC Berly Christopher Syncfusion Team January 4, 2022 02:35 PM UTC

Hi Ewerton, 
  
We will check the requested case and provide further details in two business days (6th January 2022). We appreciate your patience until then. 
  
Regards, 
Berly B.C 



SP Sureshkumar P Syncfusion Team January 7, 2022 09:08 AM UTC

Ewerton,    

Thanks for the patience.    
We have confirmed the reported issue as a bug at our end and this fix will be included in our upcoming patch release which is expected to be rolled out on end of January 25th, 2022. We appreciate your patience until then.    
You can track the status of this issue from the below feedback.  

Regards,  
Sureshkumar P 


Loader.
Up arrow icon