This is a migrated thread and some comments may be shown as answers.

Multiselect dropdown list not showing when initial value set from Model

1 Answer 815 Views
MultiSelect
This is a migrated thread and some comments may be shown as answers.
Donna Stewart
Top achievements
Rank 1
Donna Stewart asked on 15 Dec 2014, 11:07 PM
I have a multiselect in a form on a pop up window and I finally got the initial value(s) from the model showing when the window opens, but now when there is an initial value, the select list does not work.  I click in the field - no list.  It shows the list when I click in the field and there is no initial value and my placeholder text is showing.  I need help please!  I've added code below and attached screen shots.

Here is my code for the multiselect list:
<div class="form-group">
    @Html.Label("User Role(s)")
    @{
        (Html.Kendo().MultiSelectFor(model => model.SelectedRoles)
            .Name("SelectedRoles")
            .HtmlAttributes(new {style = "width: 400px"})
            .Placeholder("Please select role(s)...")
            .DataTextField("Name")
            .DataValueField("Id")
            .DataSource(source =>
            {
                source.Read(read =>
                {
                    read.Url("/api/gmcmembership/roles").Type(HttpVerbs.Get);
                });
            })
            .Value(Model.SelectedRoles)
            .AutoBind(false)
            ).Render();
    }
</div>

Here is the code for the Models:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace GMCWebApplication.Areas.admin.Models
{
    public class UserDetailViewModel
    {
        [HiddenInput]
        public string Id { get; set; }
        public int CompanyCode { get; set; }
        public string UserType { get; set; }
        public int Question1 { get; set; }
        public int Question2 { get; set; }
        public string Question1Answer { get; set; }
        public string Question2Answer { get; set; }
//        public string PasswordHash { get; set; }
        public string PhoneNumber { get; set; }
        public string NewPassword { get; set; }
        public List<UserRoleViewModel> SelectedRoles { get; set; }
        public List<UserRoleViewModel> AllRoles { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace GMCWebApplication.Areas.admin.Models
{
    public class UserRoleViewModel
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }
}

Here's the api code to get the list of all roles:

[System.Web.Http.HttpGet]
[System.Web.Http.Route("roles")]
public IHttpActionResult GetRoles()
{
    try
    {
        var list = new List<UserRoleViewModel>();
        using (var context = new ApplicationDbContext())
        {
            list = context.Roles.Select(p => new UserRoleViewModel { Id = p.Id, Name = p.Name })
                                 .ToList<UserRoleViewModel>();
        }
        return Ok(list);
    }
    catch (Exception ex)
    {
        Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
        return InternalServerError();
    }
}

Any help is very much appreciated!

Donna

1 Answer, 1 is accepted

Sort by
0
Donna Stewart
Top achievements
Rank 1
answered on 16 Dec 2014, 03:00 PM
Well, after a good night's rest, I figured out the issue.  I had Autobind set to false.  I removed this line - .AutoBind(false) - and now it works.

Thanks!
Tags
MultiSelect
Asked by
Donna Stewart
Top achievements
Rank 1
Answers by
Donna Stewart
Top achievements
Rank 1
Share this question
or