使用MVC模型来填充和验证kendo多选?

时间:2014-12-15 15:10:33

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

我正在尝试使用mvc 5将我的kendo multiselect绑定到模型中的属性,但是我得到的是一个未定义元素的列表。该列表在控制器级别是正确的,并查看源代码列表是正确的,但我无法可视化列表。

令人费解的是,列表中有更多未定义的元素,然后列出模型中的实际列表。

任何人都可以解释发生了什么,或者告诉我如何调试和解决我遇到的问题。

型号:

[Required]
public SelectList hierarchy { get; set; }

public virtual IEnumerable<SelectListItem> Hierarchy
{
    get
    {
        var hierarchies = new List<Company>();
        hierarchies = RoleCompanyHelper.GetHierachies();
        var hierarchiesList = new List<SelectListItem>();
        foreach (var hierarchy in hierarchies)
        {
            hierarchiesList.Add(new SelectListItem
            {
                Value = hierarchy.CompanyID.ToString(),
                Text = hierarchy.CompanyName
            });
        }
        return new SelectList(hierarchiesList, "Value", "Text");
    }
}

控制器:

public ActionResult Index()
{
    var vm = new AXCurrentRolesViewModel();
    return View(vm);
}

查看:

@model TelerikMvcApp1.Models.AXCurrentRolesViewModel

@(Html.Kendo().MultiSelect()
    .Name("addRoleCompany_hierarchy")
    .BindTo(new SelectList("Value", "Text"))
    .Value(Model.hierarchy)
    .DataTextField("HierarchyName")
    .DataValueField("HierarchyID")
    .Placeholder("Select Hierarchy...")
    .Filter(FilterType.StartsWith)
    .AutoBind(false)
)

稍微单独说明为什么使用模型的标准验证总是返回true?

感谢您就这些问题提供任何帮助和建议。

修改 源代码

<select id="addRoleCompany_hierarchy" multiple="multiple" name="addRoleCompany_hierarchy"></select>
    <script>
        jQuery(function(){jQuery("#addRoleCompany_hierarchy").kendoMultiSelect({"dataSource":[{"Text":"All Companies Inc IFRS \u0026 Consol","Value":"55"},
        {"Text":"All Posting Companies (exc POC \u0026 Investments)","Value":"56"},
        {"Text":"BUUK Group Structure","Value":"57"},
        {"Text":"Cardiff Entities","Value":"58"},
        {"Text":"Department","Value":"59"},
        {"Text":"GTC/GPL/ENC/IPL/QPL/EAM","Value":"60"},
        {"Text":"GTC/GUC/CUL","Value":"61"},
        {"Text":"GTCConsoleAndOperationalCompanies","Value":"62"},
        {"Text":"GTCOperationalCompanies","Value":"63"},
        {"Text":"Inexus Companies","Value":"64"},
        {"Text":"Investment Companies Only","Value":"65"}, 
        {"Text":"PIL/POL","Value":"66"}],"dataTextField":"HierarchyName","filter":"startswith","autoBind":fal    se,
        "dataValueField":"HierarchyID","placeholder":"Select Hierarchy..."});});
    </script>

1 个答案:

答案 0 :(得分:2)

更改

.DataTextField("HierarchyName") 
.DataValueField("HierarchyID") 

.DataTextField("Text") 
.DataValueField("Value")