How to popular a class with a C#string list

Asked

Viewed 45 times

-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];
}

1 answer

1


Responsible listings is not a list already populated by select and belongs to the webdeskTarefas class?

The idea was not popular webdeskTarefas.ListaResponsaveisData?

If that’s what your code really is, it doesn’t make much sense. If I understood what you wanted to do, I would do it that way:

//Considerando que o parâmetro **webDeskTarefas** tenha como atributo a lista **ListaResponsaveis** e que esteja populada com os valores do select
public async Task<IActionResult> CadastrarTarefa([FromForm] 
WebdeskTarefas webdeskTarefas)
{
    for (int i = 0; i < webdeskTarefas.ListaResponsaveis.Count; i++) //com a lista webdeskTarefas.ListaResponsaveis ja populada pelo select
    {
       Responsaveis responsavel = new Responsaveis()
       {
           login = webdeskTarefas.ListaResponsaveis[i]
       };
       webdeskTarefas.ListaResponsaveisData.Add(responsavel);
    }
}
  • 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

  • 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.

  • 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.'"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.