【问题标题】:Kendo DropdownList Set Value From ModelKendo DropdownList 从模型中设置值
【发布时间】:2014-03-28 04:40:06
【问题描述】:

这是我的实体编辑页面代码,

@(Html.Kendo().DropDownListFor(x => x.ParentCategoryId).Name("ParentCategoryId").HtmlAttributes(new { style = "width:300px;" }).DataTextField("Name").Value("ID").DataValueField("ID")
                      .DataSource(source => { source.Read(read => { read.Action("GetCategory", "Category"); }); }))

我想通过 Model.ParentCategoryId 设置下拉列表选择项

是否有可能 dropdownlist.selectedvalue = Model.ParentCategoryId ?

【问题讨论】:

  • 是的,你是对的,绑定很好。但是,在我的 editpview 中,我想通过 model.ParentCategoryId 在 kendo 下拉列表中选择项目?

标签: c# javascript asp.net-mvc model-view-controller kendo-ui


【解决方案1】:

如果您想更改下拉列表中的选定项目,您需要为下拉列表提供一些信息,以便它知道要从其数据绑定中选择哪个项目。

一个简单的 javascript 方法可能是:

var dropdownlist = $("#dropdownlist").data("kendoDropDownList");

dropdownlist.select(function(dataItem) {
    return dataItem.text === "SomeStringToMatchUp";
});

在您的情况下,您将使用 .value 而不是 .text。我并没有真正使用您的方法,因此您实际上可能必须设置 dataItem.SomeName 才能使这种方法起作用。

如果要设置默认选择,几种有效策略的一种方法是在 = 之后设置它 databinding is complete (event)

编辑:Here is another similar thread which you should reference。请记住,搜索是您最好的朋友!

【讨论】:

  • $(document).ready(function () { var dropdownlist = $("#KendoDDL").data("kendoDropDownList"); dropdownlist.select(function (dataItem) { return dataItem.Name === "isim0"; }); });我试过这样但没有说话:(
  • 您可能想要进行一些 JS 调试并查看您的 DDL 和 var 下拉列表以查看它是否正确填充。还要确保具有上述功能的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-19
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
  • 1970-01-01
  • 2022-07-29
  • 1970-01-01
相关资源
最近更新 更多