post multi-select list to list of objects asp.net core 2


I have two classes called User and Role. User is like this

class User{ ... public List<Role> Roles; }

and Role is like this

class Role{ ... public int ID; public string Name; }

now I have an action (Submit)

public IActionResult Submit(User user){ ... }

in my index.cshtml select is

<select multiple="multiple" class="multi-select" id="my_multi_select1" asp-for="Roles" asp-items="ViewBag.Roles"></select>

and ViewBag.Roles value is

ViewBag.Roles= new SelectList(Role.SelectAll(), "Name", "Name");

the problem is that when I post my form, data is sent like this ...&Roles=role1,role2&... and so user.Roles becomes null while I want to make two Roles with names role1 and role2 and assign them to user.Roles (Actually I want Asp does that)


The Select Tag Helper asp-for specifies the model property name for the select element and asp-items specifies the option elements(a collection of SelectListItem ),rather than object collection.

1.Add a ViewModel with a List as the property for your dropdown items.

public class UserViewModel { public int Id { get; set; } public string Name { get; set; } public List<int> Roles { get; set; } }

2.In.cshtml file select like below

@model PostMultiSelect.Models.UserViewModel <select multiple="multiple" class="multi-select" id="my_multi_select1" asp-for="@Model.Roles" asp-items="(List<SelectListItem>)ViewBag.Roles"></select>

3.The value of ViewBag.Roles like below, get the Roles with RoleId

ViewBag.Roles = _context.Roles.Select(r=>new SelectListItem { Value=r.ID.ToString(),Text=r.Name}).ToList();

If you want to get Roles with name ,you could change "<em>Value=r.ID.ToString()</em>" to"<em>Value=r.Name</em>"


  • Is it possible to preserve variable names when writing and reading term programatically?
  • How to use javascript or jQuery to quickly select a preset of multi-select listbox items?
  • Unable to find meaningful logs for ReactNative using Crashlytics
  • Cannot read property “length” from undefined. (line 39, file “Code”)
  • What are the precautions to be taken while creating a clearcase component?
  • PHP - How to access and retrieve important data from a pop3 email account?
  • How to get list of all user CA certificates installed in an Android Device?
  • imacros: javascript i get www._undefined_.com error
  • Google Maps V3 (PHP/MYSQL with custome infobox)
  • forever does not start on server reboot [duplicate]
  • CSV processing in Hadoop
  • MVC3 Extension for ValidatorMessage
  • Override all shipping costs for a specific shipping class in Woocommerce
  • Get the UTM tags with Facebook Marketing API
  • Why am I getting an Argument exception when creating event handler dynamically?
  • $this->a->b->c->d calling methods from a superclass in php
  • dmtracedump doesn't work, HELP!
  • Find all parks for a given zipcode with google maps
  • Use AutoIt with java applications
  • Stop an element moving with padding on hover
  • What is the difference between dynamically creating a script tag and statically embed a script tag?
  • Silverlight Event Log in Isolated Storage
  • Regex not working in java 1.5
  • Runtime complexity of getting the length of a string in different representations
  • Calculate time from document
  • 'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs
  • How can I ssh into a server that requires 2 password authentication using python's paramiko mod
  • Amazon Elastick BeanStalk error: Failed to create the AWS Elastic Beanstalk application version
  • How to turn off notice reporting in xampp?
  • Android: Unable to detect vertical plane
  • Using redis as an LRU cache for postgres
  • Send array to next viewcontroller iOs xcode [duplicate]
  • Functions by reference or by variable, which to use when?
  • Bad automatic Triangulation with Mayavi for coloring a surface known only by its corner
  • Codeigniniter insert data through models and controller
  • PHP Permalinks.. how to change?
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • ReferenceError: TextEncoder is not defined
  • Time Complexity of Fibonacci Algorithm [duplicate]
  • XSLT Transformation to validate rules in XML document