C
C#16mo ago
framoz

✅ Multiple selection input

Hello! I have a MVC Project and I wanted to select multiple people on the Create View of this object, to be part of this objects list of people. Everything in this form works, all the fields work just fine, except the multiple selection one. I managed to make it show all people available, but when I select one or multiple "Person", nothing is passed to the Create action. As everything else is working just fine, I'm guessing the problem might be in my Create view. I don't know how you do to post code here on discord, so I'll send what I have with screenshots. This first image shows the selection working fine on the view, showing both Person objects I have created
7 Replies
framoz
framoz16mo ago
this is the code on Create.cshtml (for this part)
framoz
framoz16mo ago
my action
framoz
framoz16mo ago
also this, almost forgot, here is the part where that person list get populated to be shown on the form page
framoz
framoz16mo ago
when I debug it, People is always empty (but not null), even though I select both Person 1 and Person 2
framoz
framoz16mo ago
well, after a whole day I discovered what I was doing wrong what happened is that I was trying to pass this MultiSelectList to People (that is an ICollection<Person>) and this MultiSelectList returns a string array also the code in create.cshtml was wrong
framoz
framoz16mo ago
correct code is like this
framoz
framoz16mo ago
I already created this SelectedPeople (that is a string[]) before, but the form code was wrong so it didn't worked and I started looking other stuff instead of this now all its left to do is to find People that contain the ids present in SelectedPeople array and put them in Film.People