4
I am trying to mount a select Multiple input and leave marked the options that are already present in a certain list.
I’m a beginner in C# so the logic of what I need would be more or less this: (Notice I’m using Razor)
Sorry who already answered. I transcribed the wrong code. I fixed it now:
foreach (var cc in Model.DdlCentroCusto)
{
if (listaCentrosDeCustoEquipe[].CodCentroCusto == cc.Value)
{
<option value="@cc.Value" selected="selected">@cc.Text</option>
}
else
{
<option value="@cc.Value">@cc.Text</option>
}
}
This does not work because I did not specify the index of the item, and may be several items. I’ve seen some Where code related to lists, I’ve also seen that for the list there is a Contains option (only I need to go in the specific field and pick by the string and it asks the type of the object), but I don’t know which one to use and I don’t know how to use.
I believe that it is not difficult so a help will be very welcome.
Do not use Where why it will iterate the entire list. Use Any(). Ex: listCentrosDeCustoEquipe.Any(x => x.CentroCusto.Tostring() == cc.Value)
– Marcelo de Aguiar
Great tip. In this case the list will have a maximum of 5 items but I will use!
– Joao Paulo