【问题标题】:How to bind kendo dropdownlist to model (strongly typed view)如何将剑道下拉列表绑定到模型(强类型视图)
【发布时间】:2015-01-03 08:37:58
【问题描述】:

我想以级联方式将两个剑道下拉列表绑定到强类型视图 (@model)。这个模型是一个 List:

class Enterprise
{
    string EnterpriseId {get; set;}  //Bind this to first dropdown
    List<FinYear> FinancialYears {get; set;}
}


class FinYear
{
    string FinancialYear {get; set;} //Bind this to second dropdown
    string EnterpriseId [get; set;}
}        

如何正确地从 List 中获取数据到下拉列表中?

【问题讨论】:

    标签: c# kendo-asp.net-mvc


    【解决方案1】:

    使其工作的解决方案:我使用了 javascript 和 html 的组合

    HTML

    // first dropdown
    @(Html.Kendo.DropDownList()
    .Name("entDDL")
    .DataTextField("EnterpriseId")
    .DataValueField("EnterpriseId")
    .BindTo(Model)
    .Events(e => e.Select("on_select")))
    
    <input id="fDDL"> // second dropdown
    

    Javascript

    <script>
    //on document ready
    $(document).ready(function (){
        var finYearDDL = $("#fDDL").kendoDropDownList({}).data("kendoDropDownList");});
    
    function on_select(e) {
        var dataItem = this.dataItem(e.item.index());
        dataItem = JSON.parse(JSON.stringify(dataItem.FinancialYears));
        var source = new kendo.data.DataSource({data : dataItem});
    
        // finyear dropdown
        var bind = $("#fDDL").kendoDropDownList({
            dataSource : source,
            datatextField : "FinancialYear",
            dataValueField : "FinancialYear",
            optionLabel : "Select..."});
    }
    

    【讨论】:

    • 这似乎创建了 entDDL 对象和一个名为 fDDL 的输入。我看不到 fDDL 上的数据绑定如何从 entDDL 中提取数据,也看不到 finYearDDL 的使用位置。
    【解决方案2】:

    当您将强类型模型传递给视图时,如果您将剑道控件命名为与要绑定的属性名称相同的名称,那么它将在加载视图时自动将存储的值绑定到控件。

    @model TestProject.Models.TestModel
    
    @(Html.Kendo().ComboBox()
    .Name("Model property you want to bind")
    .Text(Model.TestPropertyText) //Display text of stored value in field
    .Placeholder("Place holder text")
    //The DataText and DataValue fields bind your listItem's text and values
    .DataTextField("TestPropertyText")
    .DataValueField("TestPropertyId")
    .AutoBind(false)
    //Now I used a datasource from a server side controller action but 
    //you should be able to do this also by the .BindTo(Model) as well
    .DataSource(d => 
    { 
       d.Read(r => r.Action("GetTestModel_Read","TestController"));
    }))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 2013-04-05
      相关资源
      最近更新 更多