如何获得Kendo DropDownList的选定值

iha*_*ash 7 kendo-ui kendo-dropdown

我无法弄清楚如何确定在我的剑道下拉列表中选择了哪个项目.我的观点将其模型定义为:

@model KendoApp.Models.SelectorViewModel
Run Code Online (Sandbox Code Playgroud)

ViewModel定义为:

public class SelectorViewModel
{
    //I want to set this to the selected item in the view
    //And use it to set the initial item in the DropDownList
    public int EncSelected { get; set; }

    //contains the list if items for the DropDownList
    //SelectionTypes contains an ID and Description
    public IEnumerable<SelectionTypes> ENCTypes
}
Run Code Online (Sandbox Code Playgroud)

在我看来,我有:

@(Html.Kendo().DropDownList()
                    .Name("EncounterTypes")
                    .DataTextField("Description")
                    .DataValueField("ID")
                    .BindTo(Model.ENCTypes)
                    .SelectedIndex(Model.EncSelected)
                )
Run Code Online (Sandbox Code Playgroud)

这个DropDownList包含我期望的值,但是当用户单击提交按钮时,我需要将所选值传递回我的控制器.一切正常,但我无法访问从控制器的[HttpPost]操作中选择的项目.那么,我如何将DropDownList的值分配给隐藏的表单字段,以便它可供控制器使用?

Bri*_*Kay 20

对于任何发现这种想知道如何在JavaScript中获取所选值的人来说,这是正确的答案:

$("#EncounterTypes").data("kendoDropDownList").value();
Run Code Online (Sandbox Code Playgroud)

从文档:http://docs.telerik.com/kendo-ui/api/javascript/ui/dropdownlist#methods-value


dus*_*88c 9

当从下拉列表中选择一个值时,在selec事件中,我们可以得到如下所选的值,

@(Html.Kendo().DropDownList()
              .Name("booksDropDown")
              .HtmlAttributes(new { style = "width:37%" })
              .DataTextField("BookName")
              .DataValueField("BookId")
              .Events(x => x.Select("onSelectBookValue"))
              .DataSource(datasource => datasource.Read(action => action.Action("ReadBookDropDow", "PlanningBook").Type(HttpVerbs.Get)))
              .OptionLabel("Select"))
Run Code Online (Sandbox Code Playgroud)

javascript函数如下,

   function onSelectBookValue(e) {    

                    var dataItem = this.dataItem(e.item.index());
                    var bookId = dataItem.BookId;
                 //other user code
    }
Run Code Online (Sandbox Code Playgroud)

我相信这会对某人有所帮助

谢谢

  • 正是我需要的!谢谢 (2认同)

Lak*_*ari 5

您好,我只是在解决这个问题,继续进行了2个小时的搜索,并提出了自己的解决方案。

因此,这条线用于获取投标到剑道下拉菜单的任何数据。

$("#customers").data("kendoDropDownList").dataSource._data[$("#customers").data("kendoDropDownList").selectedIndex].colour;
Run Code Online (Sandbox Code Playgroud)

只需将id客户更改为您给kendo下拉列表提供的id。


Sta*_*zer 2

也许您应该使用 Kendo DropDownList 的 DropDownListFor 构造,如下所示:

@(Html.Kendo().DropDownListFor(m => m.EncSelected)
                    .Name("EncounterTypes")
                    .DataTextField("Description")
                    .DataValueField("ID")
                    .BindTo(Model.ENCTypes)
                    .SelectedIndex(Model.EncSelected)
                )
Run Code Online (Sandbox Code Playgroud)

这样,当您提交时,它将在 POST 请求中可用,并且您不需要在任何地方放置隐藏字段。

但是如果您出于某种原因需要使用隐藏字段,请将其放在那里,订阅下拉列表的 select 事件并使用 JQuery (例如)将所选项目放在隐藏字段上。

这是你的选择 :)

  • 其实,这是不对的。如果使用绑定到模型的 DropDownListFor,则发布请求将包含下拉列表所绑定到的模型属性上包含的下拉列表选定值。 (3认同)