Click here to Skip to main content
14,300,474 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to send all selected items from listbox to controller but it send null please help me

What I have tried:

http get action load items from database to listbox
public ActionResult SelectUser()
       {
           var courses = _context.Courses.ToList();
           var users = _context.Users.ToList();
           var viewmodel = new SelectUserViewModel
           {
               Courses =  courses,
               ApplicationUsers = users

           };
           return View(viewmodel);
       }

[HttpPost]
public ActionResult AddUser(SelectUserViewModel enrolled)
{
    return RedirectToAction("SelectUser", "Admin");
}



View
@model Elearning_system.View_Model.SelectUserViewModel
<section>
    <div class="container">
        <div class="row">
            <div class="col-lg-6 mx-auto">
                <div class="card shadow-v2">
                    <div class="card-header border-bottom">
                        <h4 class="mt-4">
                            <center>Select User</center>
                        </h4>
                    </div>
                    <div class="card-body">


                        @using (Html.BeginForm("AddUser", "Admin", FormMethod.Post, new { @class = "px-lg-4", role = "form" }))
                        {
                            
                            @Html.AntiForgeryToken()
                            @Html.ValidationSummary("", new { @class = "text-danger" })
                            //@Html.LabelFor(m => m.UserEnrolled.Course)
                            <div class="input-group input-group--focus mb-lg-4" style="width: 100%">

                                @Html.DropDownListFor(m => m.UserEnrolled.CourseId, new SelectList(Model.Courses, "Id", "CourseName"), "Select Course", new { @class = "form-control pl-2" })

                            </div>
                            <div class="input-group input-group--focus mb-lg-4" style="width: 100%">
                                @Html.ListBoxFor(m => m.UserEnrolled.UserEnrolledId, new MultiSelectList(Model.ApplicationUsers, "Id", "FullName"), new { @class = "form-control pl-2" })
                              @*  @Html.DropDownListFor(m => m.UserEnrolled.UserEnrolledId, new SelectList(Model.ApplicationUsers, "Id", "FullName"), "Select Course", new { @class = "form-control pl-2" })*@

                            </div>
                            <input type="submit" />


                        }
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>


ModelClass
public class UserEnrolled
   {
       public int Id { get; set; }

       [NotMapped]
       [Required]
       public  IEnumerable<SelectListItem> UserEnrolledId { get; set; }


       [Required]
       public int CourseId { get; set; }



       [Required]
       public Course Course { get; set; }

       [NotMapped]
       public bool Ischeck { get; set; }

       [Required]
       public ApplicationUser ApplicationUser { get; set; }

   }
Posted
Updated 11-Sep-19 0:10am
Comments
j snooze 10-Sep-19 17:20pm
   
This link might help point you in the right direction.

https://stackoverflow.com/questions/29155328/mvc-multiselectlist-posts-null-when-form-is-submitted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

I think the the cause is

@Html.ListBoxFor(m => m.UserEnrolled.UserEnrolledId, new MultiSelectList(Model.ApplicationUsers, "Id", "FullName"), new { @class = "form-control pl-2" })

UserEnrolledId should be of same type that your ApplicationUsers's Id type is.

e.g. if it's
int
then

public  IEnumerable<int> UserEnrolledId { get; set; }

Here is a working example
ViewModel
public IEnumerable<int> BranchIds { get; set; }
public List<system.web.mvc.selectlistitem> Branches { get; set; }

View
@Html.ListBoxFor(m => m.BranchIds, new MultiSelectList(Model.Branches, "value", "text"), new { @class = "form-control pl-2" })
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100