Preselect Items in Multiselect-Listbox (MVC3 Razor)?

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

Up vote 8 down vote favorite 2 share g+ share fb share tw.

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 _selectedFoos; private readonly IList _allFoos; public IList SelectedFoos{ get{ return _selectedFoos;} } public IList 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.

Thanks in advance Tobi asp. Net-mvc asp. Net-mvc-3 razor link|improve this question edited May 2 '11 at 16:34Danny Tuppeny4,92621154 asked May 2 '11 at 14:54Tobias289212 70% accept rate.

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!

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()). – Tobias May 3 '11 at 6:17 Weird. Can't explain the ToString thing, but glad you got it sorted :-) – Danny Tuppeny May 3 '11 at 7:24 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. – Tobias May 6 '11 at 8:26.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions