简体   繁体   English

Telerik 下拉列表未正确填充

[英]Telerik DropDown List not populating correctly

My razor code icludes this telerik dropdown list:我的剃刀代码包括这个 Telerik 下拉列表:

<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.实际上,我想从这个 BkAccounts 类中删除 Id 字段,但我无法让 DDL 正确填充。 What did I miss?我错过了什么? According to Telerik doc, TextField can accept a string field so it should not be a format problem...根据 Telerik 文档,TextField 可以接受字符串字段,因此它不应该是格式问题......

Edit: the difference between the 2 snippets stands here: ValueField="@(nameof(BkAccounts.BIC) ---> does NOT populate ValueField="@(nameof(BkAccounts.Id) ---> populates like a charm编辑:两个片段之间的区别在这里: ValueField="@(nameof(BkAccounts.BIC) ---> 不填充 ValueField="@(nameof(BkAccounts.Id) ---> 填充就像一个魅力

I got the answer from Telerik forum: https://www.telerik.com/forums/telerikdropdownlist-throwing-a-system-invalidcastexception-when-valuefield-is-a-string#tnrnhB1ghkmWxEUE24wUhA我从 Telerik 论坛得到了答案: 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我需要添加到我的 TelerikDropDownList 代码中:@bind-Value=@SelectedBankAccount 并将其声明为视图模型中的字符串属性

I start to strongly suspect that the issue is on Telerik side.我开始强烈怀疑问题出在 Telerik 方面。 Here is their sample TelerikDropDownList code from https://demos.telerik.com/blazor-ui/dropdownlist/overview这是他们来自https://demos.telerik.com/blazor-ui/dropdownlist/overview的示例 TelerikDropDownList 代码

@page "/dropdownlist/overview"
@page "/dropdownlist/index"

@using TelerikBlazorDemos.Shared

<div class="example-box-wrapper">
    <div class="example">
        <div class="mb-4">T-Shirt size:</div>
        <TelerikDropDownList Data="@Data"
                             @bind-Value=@SelectedSizeMetric
                             PopupHeight=""
                             DefaultText="Select your T-shirt size"
                             ValueField="SizeMetric" TextField="SizeText">
        </TelerikDropDownList>
    </div>

    <div class="ml-4">
        Selected Size Number: <strong>@SelectedSizeMetric</strong>
    </div>
</div>

@code  {
    public IEnumerable<Size> Data { get; set; }
    public bool AllowCustom { get; set; } = true;
    public int? SelectedSizeMetric { get; set; }
    public string SelectedSize { get; set; }

    public class Size
    {
        public string SizeText { get; set; }
        public int? SizeMetric { get; set; }
    }

    protected override void OnInitialized()
    {
        List<Size> sizes = new List<Size>();

        sizes.Add(new Size()
        {
            SizeText = "X-Small",
            SizeMetric = 3
        });

        sizes.Add(new Size()
        {
            SizeText = "Small",
            SizeMetric = 6
        });

        sizes.Add(new Size()
        {
            SizeText = "Medium",
            SizeMetric = 8
        });

        sizes.Add(new Size()
        {
            SizeText = "Large",
            SizeMetric = 10
        });

        sizes.Add(new Size()
        {
            SizeText = "X-Large",
            SizeMetric = 12
        });

        Data = sizes.AsQueryable();
        base.OnInitialized();
    }
}

It works fine as it is.它可以正常工作。 But if I change:但如果我改变:

ValueField="SizeMetric" //int type

for:为了:

ValueField="SizeText"  //string type

it nolonger works... I get: System.InvalidCastException : 'Unable to cast object of type 'System.String' to type 'System.Nullable`1[System.Int32]'.'它不再有效......我得到:System.InvalidCastException:'无法将'System.String'类型的对象转换为'System.Nullable`1[System.Int32]'。'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM