Bring selected manually filled combo

Asked

Viewed 82 times

3

I’m having difficulty creating a combo of "Enabled" and "Disabled" passing the value of the bank to bring it filled. In my controller, I created the following code:

inserir a descrição da imagem aqui

In my View, following code: inserir a descrição da imagem aqui

The result is that, with the code running, it will always appear "Enabled" there in the combo.

  • 1

    The gestor.Status is a bool or an int?

  • It’s a bool @Filipeoliveira

1 answer

1

Like the gestor.Status is a bool, or you change the Value for bool:

lista.Add(new SelectListItem { Text = "Habilitado", Value = "True" });
lista.Add(new SelectListItem { Text = "Desabilitado", Value = "False" });

Or convert your own gestor.Status for whole:

ViewBag.Status = new SelectList(lista, "Value", "Text", Convert.ToInt32(gestor.Status));
  • I changed Value to "True" and "False". When saving, it works perfectly, passing the correct value, as selected. However, when editing, it does not bring the selected, according to the bank... Do you have any idea what might be? Currently I’m passing the Viewbag like this: ViewBag.Status = new SelectList(lista.AsEnumerable(), "Value", "Text", gestor.Status);

  • @Igormacedo, the gestor.Status is False really coming? By the way, I can’t imagine what the problem is, if the boleano is correct, it will display correct as well

  • @Filipeoliveria, I debugged and passed the controller with the right value. However, when I edit, it goes through the controller with the right value, but when it comes to viewing, it does not mark the one that is selected, as if it were without the 4th parameter: gestor.Status

  • @Igormacedo, is the list being filled in the same way? Value is correct?

  • @Felipeoliveira, you are. When entering a new record, it inserts as I selected in the combo. Just in time to edit that it does not bring the selected :/

  • @Igormacedo, the ideal would be to check the code. Could you edit your question by showing an example of how the insert and edit actions and their views are doing? And put in text even, not image.

Show 1 more comment

Browser other questions tagged

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