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

本文关键字:模型 强类型 视图 绑定 下拉列表 | 更新日期: 2023-09-27 18:36:13

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

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;}
}        

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

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

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

爪哇语

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

将强类型模型传递给视图时,如果将 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"));
}))