does anyone know how to initialise a MultiSelectList as empty?
I have a ListBoxFor control (MVC3) which i wish the user to populate by using other text and button controls on the web page.
It doesn’t like me commenting out the line below….
many thanks in advance
Items = new MultiSelectList(
new[]
{
//new { Id = 1, Name = "item 1" }
},
"Id",
"Name",
selectedItemIds
)
When you create a MultiSelectList like that, one of the things the compiler checks is if there are indeed
IdandNamefields in the list (IEnumerable) you provided.The problem with your code is that there are no elements named
IdandNamewhen you send an empty array of anonymous objects, therefore the compiler cannot conclude that this is valid code.If you want to be able to send an empty array, you will have to refrain from using anonymous types.
Try this:
Now you can create your MultiSelectList as follows:
I left out the selecteditemIDs because there is no use for it when the list is empty, it can only create issues.
Although I am still not certain why you want to do this. I suspect there is a better way, if you can elaborate your intentions.