Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 593
LoftyTowers Avatar asked Dec 15 '14 15:12

LoftyTowers


1 Answers

Change

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

.DataTextField("Text") 
.DataValueField("Value")
like image 100
Scottie Avatar answered Nov 08 '22 09:11

Scottie