如何将kendo下拉列表绑定到模型(强类型视图)

时间:2015-01-03 08:37:58

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

我希望以级联方式将两个kendo下拉列表绑定到强类型视图(@model)。此模型是List< Enterprise>:

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&lt; FinYear&gt;中正确获取数据进入下拉列表?

2 个答案:

答案 0 :(得分:4)

使其工作的解决方案:我使用了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..."});
}

答案 1 :(得分:1)

如果将kendo控件命名为想要绑定的属性名称,则将强类型模型传递给视图时,它会在加载视图时自动将存储的值绑定到控件。

@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"));
}))