-1
On the system I’m developing I send a select with multiple options to a list of string type in my controller, I now need to link this list I get from the view to the list with the login class I have in my model. The goal is that each position of the string list becomes a position in the new list.
For example I get the following list of values that were sent from the view select:
[0] = "147"
[1] = "56"
I need to link this array to my other login list, something like this
Login[0] = "147
Login[1] = "56"
I’ll put my codes with comments to explain better:
View:
<select class="selectpicker" multiple data-live-search="true" asp-for="ListaResponsaveis">
<option value="147">João</option>
<option value="212">Maria</option>
<option value="33">Luiza</option>
</select>
Model:
//Aqui recebo as opções marcadas no select
public List<string> ListaResponsaveis { get; set; }
//Quero passar a lista que recebi da view para essa lista
public List<Responsaveis> ListaResponsaveisData { get; set; }
public class Responsaveis
{
public string Login { get; set; }
}
Controller:
public async Task<IActionResult> CadastrarTarefa([FromForm] WebdeskTarefas webdeskTarefas)
{
//Criei essa variavel para receber os dados da outra lista
var ListaResponsaveis = webdeskTarefas.ListaResponsaveisData;
//Aqui tentei vincular cada possição do array da lista da view a minha lista, porém ele não mostra valor no webdeskTarefas.ListaResponsaveis[i]
for (int i = 0; i < webdeskTarefas.ListaResponsaveis.Count; i++)
{
ListaResponsaveis[i].Login = webdeskTarefas.ListaResponsaveis[i];
}
Yes, Responsibles is already populated, but I want to pass its content to the webdeskTarefas.Responsibleave, since I can’t do it straight from the view to the controller. Then I need each array of the webdeskTarefas.Responsable listThis is filled with the corresponding value of the Responsable Lists. Your code gave the same problem as mine, webdeskTarefas.Responsable[i] listings always comes with the value "0", it is not taking the value of the position in the array
– João
Well, I believe that there are no errors in the popular part of the List<Responsaveis>Listresponsaveisdata with the elements of the List<string>Responsibles that would be the subject of the question. It is probably only getting '0' because the list was not populated correctly, it must have instantiated the list positions but no value was assigned. Check debugging step by step if webdeskTarefas.Responsibles Listings really arrives populated with the correct values to the Register Task method.
– Felipe Mateus
Debugging here found that when it tries to add I get the error "System.Nullreferenceexception: 'Object Reference not set to an instance of an Object.'"
– João