Value and ID at Dropdown Waterfall

Asked

Viewed 70 times

1

Reference that can help you understand the code.

Dropdown Cascata

This part here I pass the values that will be added in the second Dropdown

var classesList = this.GetClasses(Convert.ToInt32(CanalMassivo));
    var classesData = classesList.Select(m => new SelectListItem()
    {
        Value = m.idSegmento.ToString(),
    });

    var states = classesData.Select(m => m.Value).ToList();
    return Json(states, JsonRequestBehavior.AllowGet);

In part Value = m.idSegmento.ToString() and the value that appears in Dropdown when the controller receives.

How could I pass the description in Dropdown and the controller receive the id value ?

I tried so

        Text = m.descricao.ToString(),
        Value = m.idSegmento.ToString(),
    });

    var states = classesData.Select(m => m.Text).ToList();

But it makes that mistake

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'idCanalIndicadoresMassivo'.
  • Post as is your view with the DropDown. You are probably using idCanalIndicadoresMassivo when it should just be Value.

  • Randrade, thank you. Could you access here? http://answall.com/questions/100716/dropdown-cascata which has all the beginning of my code, View, Controlle, Model, Javascript

1 answer

2


If you want to use the SelectListItem() change your action to this:

var classesList = this.GetClasses(Convert.ToInt32(CanalMassivo));
    var classesData = classesList.Select(m => new SelectListItem()
    {
        Value = m.idSegmento.ToString(),
        Text = m.descricao
    });
    //Não selecione somente o texto, você precisará dos dois.
    return Json(states, JsonRequestBehavior.AllowGet);

And on your call ajax, where people the DropDown, you change to pass the idSegmento in value and the descricao in the option, in this way:

   $.each(data, function (i, state) {
         //Create new option
         var option = $('<option value="'+state.Value+'">' +state.Text+ '</option>');
         //append state states drop down
         stateDropdown.append(option);
   });

This way you will view the description, and the DropDown will have the value of idSegmento.

  • Randrade.! Perfect.!! Thank you very much for your help.!!

Browser other questions tagged

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