Binding to non nullable integers

When trying to bind the value to a non nullable integer 

 <SfDropDownList TItem="Brand" TValue="int?" PopupHeight="230px" Placeholder="Select a game" @bind-Value="@product.BrandId" DataSource="@listBrands">
                                                            <DropDownListFieldSettings Text="Name" Value="BrandId"></DropDownListFieldSettings>
</SfDropDownList>

we get 

blazor.server.js:8 Uncaught (in promise) Error: Newtonsoft.Json.JsonSerializationException: Error converting value {null} to type 'System.Int32'. Path 'value', line 1, position 13.
 ---> System.InvalidCastException: Null object cannot be converted to a value type.
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)

We've tried setting TValue to both int and int? neither seems to work?



3 Replies

SP Sureshkumar P Syncfusion Team May 19, 2020 08:45 AM UTC

Hi Alex, 
 
Greetings from Syncfusion support.  
 
Based on your shared information, we have validated with our component, but we cannot able to replicate the issue from our end. please provide the issue replication code example to replicate the issue.  
 
Kindly refer the code example. 
 
<SfDropDownList TItem="GameFields" TValue="int?" PopupHeight="230px" Placeholder="Select a game" @bind-Value="@BrandId" DataSource="@Games"> 
    <DropDownListFieldSettings Text="Text" Value="ID">DropDownListFieldSettings> 
SfDropDownList> 
 
@code 
{ 
 
    public int? BrandId { get; set; } = 8; 
    public class GameFields 
    { 
        public int ID { get; set; } 
        public string Text { get; set; } 
    } 
    private List Games = new List<GameFields>() { 
        new GameFields(){ ID= 1, Text= "American Football" }, 
        new GameFields(){ ID= 2, Text= "Badminton" }, 
        new GameFields(){ ID= 3, Text= "Basketball" }, 
        new GameFields(){ ID= 4, Text= "Cricket" }, 
        new GameFields(){ ID= 5, Text= "Football" } 
     }; 
 
} 
 
 
We have prepared the sample based on your scenario. Please find the sample here: https://www.syncfusion.com/downloads/support/directtrac/general/ze/DDL-1046407152  
 
If still you have facing the issue, then please replicate the issue in our attached sample and revert us with detailed issue replication procedure. That will help us to provide exact solution as earlier as possible.  
 
Regards, 
Sureshkumar p 



AL Alex May 27, 2020 07:47 AM UTC

Hi,

Thanks for the reply. 

Not sure you've understood the issue as you are using nullable int to demonstrate this is working?  If you have no control over the data returned (e.g. from an external API) then non nullable integers don't seem to work as detailed here



SP Sureshkumar P Syncfusion Team May 28, 2020 07:01 AM UTC

Hi Alex,  
  
Thanks for your update.   
  
As per our previous mentioned forum (153844) update, our dropdownlist component with integer data types is not able to render with without nullable data type. Because the integer data type is having default value as 0. So, we cannot set the null value when reset the component value. Otherwise when you have always value as value property then you can render the component without nullable property.   
  
Regards,  
Sureshkumar P 


Loader.
Up arrow icon