none
MVC HtmlHelper ListBoxFor and ListBox MultiSelectList Bug RRS feed

  • General discussion

  • The HtmlHelper.ListBoxFor and HtmlHelper.listBox methods have a design bug.  A very frustrating and annoying bug, as no error is thrown, it simply doesn't correctly display the selected values.

    I'm not looking for an answer, as I've already figure it out.  I'm posting so others don't have to spend hours debugging.

    Simply put, whomever designed these HtmlHelpers messed up.

    Here is what you need to know and understand.

    1) The selectedValues parameter must be populated with a collection of key values only.  It cannot be a collection of the selected objects, as the HtmlHelper does not apply the dataValueField to this collection for you.

    2) If using the ListBox you cannot set the name parameter the same as a Model property.  Also you cannot name the ViewBag property that will contain the collection of items the same as the Model property.

    3) If  using the ListBoxFor it gets even more wonky.  You should name the ViewBag property that will contain the collection of items the same as the Model property name.  Now when you use the ListBoxFor within the View you must use a ViewBag property that does not exist (this is important!).  The HtmlHelper.ListBoxFor will look automatically for a ViewBag property with the assigned Model property name.

    Code Example below;

    public class Student
    {
     public string Id { get; set; }
     public string Name { get; set; }
     public Student(string id, string name) {
      this.Id = id;
      this.Name = name;
     }
    }
    
    public class Room {
     public List<Student> Students { get; set; }
    }
    
    public ActionResult() {
     var students = new List<Student>() {
      new Student("1","test1"),
      new Student("2","test2"),
      new Student("3","test3")
     }
    
     var room = new Room();
     room.Students = students.Skip(2).Take(2);
    
     // Make sure the selected list only contains the Ids.
     var selected = room.Students.Select(s => s.Id);
    
     // Make sure the ViewBag property is the same name as the Model property.
     this.ViewBag.Students = new MultiSelectList(students, "Id", "Name", selected);
    
     return View(room);
    }
    
    // In the View
    @model Room
    
    @Html.ListBoxFor(m => m.Students, this.ViewBag.JustMakeUpANameHere as MultiSelectList)

    I've tested this scenario.  It's strange, but it works.  So for those of you losing your hair trying to figure out why it refuses to preselect the selected items, now you know how to workaround the issue.

    • Changed type Jeremy Foster Saturday, January 12, 2013 6:42 PM I'm not looking for an answer
    Saturday, January 12, 2013 6:40 PM

All replies

  • Hi Jeremy,

    Thank you for sharing this workaround.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 14, 2013 8:09 AM
    Moderator
  • I encountered this bug today. Thank god I found your post. This is so horrible that it should be fixed as soon as possible.

    Monday, March 25, 2013 4:19 PM
  • Very helpful.  Thanks for posting!!
    Thursday, October 17, 2013 2:59 PM
  • Just wanted to say THANK YOU!! I spent hours on this bug and it nearly drove me crazy. 
    Wednesday, November 6, 2013 7:39 AM
  • I could have searched a looooonnnnngg time for that one! You know how it is when you're learning something new and you hit a bug like that, pretty painful!

    Thanks for sharing!


    Mathieu Isabel

    Tuesday, December 10, 2013 2:39 AM
  • OMG!!! This bug kicked my butt from 1 PM yesterday to 3:37 PM today....one whole day of trying different things and thought I was loosing my mind. Thanks for posting..

    Gordsh

    Tuesday, December 31, 2013 8:28 PM
  • Please post ASP.NET/MVC questions and discussions in the ASP.NET forums (http://forums.asp.net ).
    Tuesday, December 31, 2013 9:45 PM
    Moderator
  • Very helpful!  It should be noted that this HtmlHelper works as intended in MVC3.  I ran across this problem after I upgraded my application to MVC4.  Thank you.
    Thursday, February 27, 2014 8:31 PM
  • Hi Jeremy

    Thanks a lot! You helped me save valuable time.

    I wonder why this is still not fixed. I use MVC 5 and your solution is still needed to make the selection work in multiselectlist. If anyone at Microsoft really cares, they should act as you said over 1 year ago... :-(

    Sunday, April 20, 2014 4:59 PM
  • WOW, the time I spent before finding this post....

    Thanks alot!!

    Thursday, April 28, 2016 3:52 PM
  • You Rock!  I spent way too long on this problem and want to thank you so much for posting.  I turned to the MultiSelectList after spending a couple of days trying to get a JQuery dropdownlist plugin implemented to no avail.  I thought the MVC HtmlHelper would be a no-brainer and was kicking myself for not going that route in the first place.  If I had only known!!!  TGFI  (Thank God for the Internet!).  
    Tuesday, September 6, 2016 6:57 PM
  • Thank you Jeremy. 

    I've spent few days trying to make it works and who knows that to populate ListBoxFor correct you should define not existed item. 

    Thursday, November 24, 2016 8:30 AM
  • Thanks Jeremy! 
    Thursday, May 18, 2017 9:35 PM