Dropdownlistfor has no selected value

Asked

Viewed 193 times

1

Dropdownlistfor shows no selected value.

 public static List<SelectListItem> getMesesPagamento()
    {
        List<SelectListItem> listMeses = new List<SelectListItem>();

        for (byte i = 1; i <= 12; i++)
        {
            if (i == 6) // Junho
                listMeses.Add(new SelectListItem { Text = Utilitario.getMes(i), Value = i.ToString(), Selected = true });
            else
                listMeses.Add(new SelectListItem { Text = Utilitario.getMes(i), Value = i.ToString() });
        }
        return listMeses;
    }

Na View:

@Html.DropDownListFor(model => model.Pagamento_MesReferencia, Model.MesesPgto, "", htmlAttributes: new { @id = "ddlMesesPgto" })

inserir a descrição da imagem aqui

List Type

model.Pagamento_MesReferencia é do tipo List<SelectListItem>
  • It does not appear in the View the value you marked in the controller as SELECTED?

  • Does not appear selected in View

  • Cool this I already know, what does not appear, is what Voce marked Selected colo in the code? it appears as? has how to put print?

  • I edited the question and posted the print @Paulohdsousa See that in the code the month is not selected.

  • Where do you add the model to your list? puts the code

  • public List<Selectlistitem> Mesespgto { get; set; }

Show 1 more comment

2 answers

1

This happens because you are using the wrong component. It is useless to generate the list with Selected set in server where the value of Pagamento_MesReferencia is not equal to 6.

The correct is you set the value of Pagamento_MesReferencia as a whole and assemble the DropDownList in View:

@Html.DropDownListFor(model => model.Pagamento_MesReferencia, Enumerable.Range(1, 12).Select(option => new SelectListItem
{
    Text = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.GetMonthName(option),
    Value = option.ToString(),
    Selected = (Model != null) && (Model.Pagamento_MesReferencia == option)
}), "Indefinido", new { @class = "form-control mes" })
  • 1

    Looking better if "model.Payment_mesref" is not integer will do nothing to put the integer 6 as selected. Thanks Gypsy for the clarification. My reply was given on a test basis and "model.Payment_mesreferencia" was with whole.

0

Tell me that in the controller you put true (it was supposed to work), but it didn’t work here, take a look there. I took the test here and I decided when I do so:

 public static List<SelectListItem> getMesesPagamento()
    {
        List<SelectListItem> listMeses = new List<SelectListItem>();

        for (byte i = 1; i <= 12; i++)
        {
            if (i == 6) // Junho
                listMeses.Add(new SelectListItem { Text = Utilitario.getMes(i), Value = i.ToString(), i });
            else
                listMeses.Add(new SelectListItem { Text = Utilitario.getMes(i), Value = i.ToString() });
        }
        return listMeses;
    }

When I enter the number of the object, I do not know why true did not work with me too, I had implementations that worked, if I am not mistaken, I looked here and did not find.

Browser other questions tagged

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