How to select value in Select

Asked

Viewed 706 times

2

How to select a value that is not the first according to an ASP.NET variable Razor C#.

<select id="MeetingFrequency" name="MeetingFrequency">
   <option value="0" selected disabled>Selecionar</option>
   <option value="SEMANAL">Semanal</option>
   <option value="MENSAL"> Mensal</option>
   <option value="BIMESTRAL" >Bimestral</option>
   <option value="TRIMESTRAL">Trimestral</option>
   <option value="QUADRIMESTRAL">Quadrimestral</option>
   <option value="SEMESTRAL">Semestral</option>
   <option value="ANUAL">Anual</option>
</select>
  • How to select a value according to a variable?

  • Good ė a value that comes from the bank. But I’ve done it in a way that worked

  • You managed to solve ? where the information comes from, could put the whole logic in your question!?

3 answers

2

To maintain a value default in the component <select> you must use the property <selected> in the <option>

Use of that command!

<option value="audi" selected>Audi</option>

See the first line of your component option <select> you are already using the property <selected> so he comes selected!

See Example

2

Can be done with Razor through the Dropdownlistfor and by Selectlist.

 @Html.DropDownListFor(model => model.Variavel, new SelectList(Model.ListaVariavel, "Value", "Text", Model.Variavel), "Selecione")

In the code, if model.Variavel has value, it will set the value for you. If you need another value, put in the Model.Variavel (third parameter of the selectlist)

I suggest you see about Selectlist and on Dropdownlistfor

If necessary, speak to me and I will set a practical example for you.

2


Well once I used something like this, see if it helps you!

@{  
bool[] select = new bool[2];
for (int i = 0; i < 2; i++)
{
select[i] = false;
}
if(model.valor != null)
 switch (model.valor.ToString())
{
 case "SEMANAL": select[0] = true; break;
 case "MENSAL": select[1] = true; break;
 }
 }
}
<select id="MeetingFrequency" name="MeetingFrequency">
<option value="0"selected disabled>Selecionar</option>
<option value="SEMANAL"selected=@select[0]>Semanal</option>
<option value="MENSAL" selected=@select[1]> Mensal</option>
</select>

Give one adapted to your need

Browser other questions tagged

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