简体   繁体   中英

Using an MVC model to populate and validate a kendo multiselect?

I am trying to bind my kendo multiselect to a property in a model using mvc 5 however all i get is a list of undefined elements. The list is correct at the controller level and looking at the source code the list is correct there but I can not visualize the list.

What is also puzzling is that there are more undefined elements in the list then items the actual list in the model.

Can anyone explain what is going on or show me how to go about debugging and fixing the issues I am having.

Model:

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

Controller:

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

View:

@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)
)

On a slightly separate note why does my standard validation using the model always return true??

Thank you for any help and advice on these issues.

EDIT Source code

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

Change

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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