vendredi 30 septembre 2016

Why is my MultiSelectList not returning data to model?

I have the following View Model:

public class SendGroupEmailViewModel
{
    public int EmailID { get; set; }
    public IEnumerable<SelectListItem> Roles { get; set; }
    public int[] SelectedRoles { get; set; }

}

The controller action that sends the appropriate information to the view:

var viewModel = new SendGroupEmailViewModel
        {
            Roles = db.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Id.ToString(), Text = rr.Name }).ToList(),

        };

Then in my view:

@Html.ListBoxFor(model => model.SelectedRoles, new MultiSelectList(Model.Roles, "Value", "Text"), new { @class = "" })

The list appears correctly within the view but when the form with the above ListBoxFor in is posted SelectedRoles returns as null.

Can anyone tell me why this is and how I can remedy it?

Aucun commentaire:

Enregistrer un commentaire