简体   繁体   English

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

[英]How to bind kendo dropdownlist to model (strongly typed view)

I wanted to bind two kendo dropdownlists to the strongly typed view (@model) in a cascading manner. 我想以级联方式将两个kendo下拉列表绑定到强类型视图(@model)。 This model is a List<Enterprise>: 此模型是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;}
}        

How to properly get data from List<FinYear> into the dropdown? 如何正确地将List <FinYear>中的数据导入下拉列表?

The solution to making it work: I used a combination of javascript and html 使它工作的解决方案:我使用了javascript和html的组合

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 使用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..."});
}

When you pass a strongly typed model to the view if you name a kendo control the same as the property name that your wanting to bind to then it will automatically bind the stored value to the control as the view is loaded. 当您将强类型模型传递给视图时,如果您将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"));
}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM