Search code examples
c#telerikblazor

Telerik DropDown List not populating correctly


My razor code icludes this telerik dropdown list:

<TelerikDropDownList Data="@BkAccounts" TextField="@(nameof(BkAccounts.DisplayField))" ValueField="@(nameof(BkAccounts.BIC))" Value=1 ></TelerikDropDownList>

It points to this class:

public class BkAccounts
{
    public int Id { get; set; }
    public string Iban { get; set; }
    public string BIC{ get; set; }
    public string DisplayField{ get { return this.Iban + " - " + this.BIC; } }
}

If I write it this way, the dropdown list does not populate correctly.

If I change it this way:

<TelerikDropDownList Data="@BkAccounts" TextField="@(nameof(BkAccounts.DisplayField))" ValueField="@(nameof(BkAccounts.Id))" Value=1 ></TelerikDropDownList>

It works fine.

Actually, I want to remove the Id field from this BkAccounts class but I am not able to have the DDL to populatecorrectly. What did I miss? According to Telerik doc, TextField can accept a string field so it should not be a format problem...

Edit: the difference between the 2 snippets stands here: ValueField="@(nameof(BkAccounts.BIC) ---> does NOT populate ValueField="@(nameof(BkAccounts.Id) ---> populates like a charm


Solution

  • I got the answer from Telerik forum: https://www.telerik.com/forums/telerikdropdownlist-throwing-a-system-invalidcastexception-when-valuefield-is-a-string#tnrnhB1ghkmWxEUE24wUhA

    I needed to add into my TelerikDropDownList code: @bind-Value=@SelectedBankAccount and declare it as a string property into theviewmodel