将 Kendo dropdownlistfor 的默认值设置为 'Selected = true' SelectListItem

Set default value of Kendo dropdownlistfor to 'Selected = true' SelectListItem

我有以下 Kendo 下拉列表:

@(Html.Kendo().DropDownListFor(m => Model.CmtsId).Name("#=uid#CmtsId")
                            .Animation(false)
                            .DataSource(a => a.Read(r => r.Url("/webAPIpath")))
                            .DataTextField("Text")
                            .DataValueField("Value")                                
                            .OptionLabel("Select")
                            .ToClientTemplate())

以下代码位于 "webAPIpath" 的另一端,并为上述下拉列表构建一个 SelectList 以用作选项列表:

var results = await _cmtsService.GetAllCmts(LogData);
        if (results.Status == ResponseStatus.Success)
        {
            var markets = await _locationService.GetMarketsForHeadEndAsync(headEnd, LogData);
            var prevSelected = await _pendingChangesService.GetPendingPortSelections(nodeId, LogData);
            var list = new List<SelectListItem>();
            foreach (var cmts in results.Data.Where(a => markets.Data.Any(b => b.Id == a.MarketId)))
            {
                var market = markets.Data.FirstOrDefault(a => a.Id == cmts.MarketId);
                var mfg = _deviceService.GetManufacturerAsync(cmts.ManufacturerId, LogData);
                var model = _deviceService.GetModelAsync(cmts.ModelId, LogData);                    
                await Task.WhenAll(mfg, model);
                list.Add(new SelectListItem()
                {
                    Text =
                        $"cmts{cmts.Number}-{market.MarketAbbreviation} / {mfg.Result.Data.Manufacturer} / {model.Result.Data.Model} / {cmts.DocsisCapability}",
                    Value = cmts.Id.ToString()
                });
            }

            //Set selected value if previous selection exists
            foreach (var item in list.Where(item => item.Value.Equals(prevSelected.Data?.NewCMTSID.ToString())))
            {
                item.Selected = true;
            }

            return Ok(list);
        }
        return InternalServerError(results.Exception);

我想要的是 list 中的 item,其中 item.Selected = true; 是我的下拉列表的默认值。我这辈子都想不出如何让 kendo 下拉菜单尊重它绑定到的选择列表中项目的选定状态。我找到的所有 kendo 文档都告诉我使用 .Value().SelectedIndex() 但我不能这样做(或者不知道我会怎么做)因为 'Selected' 列表中的项目是通过单独的数据库查询动态确定的,因此我必须进行单独的调用,然后进行比较并通过 javascript/jquery.

设置值

简而言之,如何让 Kendo().DropDownListFor 将默认选择的选项设置为我的 SelectList 中的项目 Selected = true;

如果您要绑定到一个原始值(看起来是),您需要添加一个属性(注意下划线,而不是破折号):

@(Html.Kendo().DropDownListFor(m => Model.CmtsId).Name("#=uid#CmtsId")
                            .Animation(false)
                            .DataSource(a => a.Read(r => r.Url("/webAPIpath")))
                            .DataTextField("Text")
                            .DataValueField("Value")                                
                            .OptionLabel("Select")
                            .HtmlAttributes(new { data_value_primitive = "true" })
                            .ToClientTemplate())

http://docs.telerik.com/kendo-ui/framework/mvvm/bindings/value#use-the-value-binding-with-a-select-widget-to-update-the-view-model-field-with-the-value-field-when-the-initial-value-is-null