Preselect Items in Multiselect-Listbox (MVC3 Razor)
Asked Answered
T

2

18

I have a problem with the preselection of Items in a listbox. I am using razor view engine with mvc 3. I know there are a few posts with the same issue but they don't work for me.

Code in Class:

public class Foo{
    private int _id;
    private string _name;

    public string Name{
       get{
           return _name;
       }

    public int Id {
       get{
           return _id;
       }

}

Code in Model:

public class FooModel{

    private readonly IList<Foo> _selectedFoos;
    private readonly IList<Foo> _allFoos;

    public IList<Foo> SelectedFoos{
         get{ return _selectedFoos;}
    }

    public IList<Foo> AllFoos{
         get{ return _allFoos;}
    }

}

Code in cshtml:

 @Html.ListBoxFor(model => model.Flatschels, 
        Model.AllFlatschels.Select(fl => new SelectListItem {
             Text = fl.Name,
             Value = fl.Id.ToString(),
             Selected = Model.Flatschels.Any(y => y.Id == fl.Id)
   }), new {Multiple = "multiple"}) 

I tried lots of other things but nothing worked. Hope someone can help.

Toadstool answered 2/5, 2011 at 14:54 Comment(0)
H
30

I can't really explain why, but I managed to get it working. Either of these worked:

@Html.ListBoxFor(m => m.SelectedFoos,
            new MultiSelectList(Model.AllFoos, "ID", "Name"), new {Multiple = "multiple"}) 

@Html.ListBoxFor(m => m.SelectedFoos, Model.AllFoos
            .Select(f => new SelectListItem { Text = f.Name, Value = f.ID }),
                new {Multiple = "multiple"}) 

The problem seems to be that the Selected property on SelectListItem is ignored, and instead the ToString()(!) method is being called, so if you need to add this to your Foo class:

public override string ToString()
{
    return this.ID;
}

I'm guessing it has something to do with being able to persist across requests (which will be flattened to strings to be passed over the wire), but it's a bit confusing!

Hers answered 2/5, 2011 at 16:32 Comment(5)
Thanks a lot. I got it working. Although only the second code worked and it seems to me, that it is not because of the overridden ToString(). My Id Property was an int and I added a Property Key returning a unique string (guid.ToString()).Toadstool
Weird. Can't explain the ToString thing, but glad you got it sorted :-)Hers
It was definitly because of the ToString(). Just posted a Question in the Microsoft Forum and hope to get an answer and maybe an info why (and how long) this is they way it currently is.Toadstool
Same behavior on Razor for MVC4. Borderline on bug (at least for ListBox(), which does not expect a Model to compare elements on).Statuary
Worked for me, too! For whatever reason, the following did not work: @Html.DropDownListFor(m => m.SelectedFoos, new MultiSelectList(Model.AllFoos), new {multiple = ""} (where in my case, Foo is an enum, not a tuple object. The only change I needed to make was DropDownListFor -> ListBoxForExpressly
F
0

In MVC5 you can directly use ListBoxFor with multiselect. Make sure while loading the view your selectedItem should have list of items.

@Html.ListBoxFor(m => m.SelectedItem, new MultiSelectList(Model.Item.ToList(), "Value", "Text"), new { @class = "form-control" })
Finagle answered 19/12, 2017 at 0:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.