How to get selected value from dropdownlist in Asp.Net MVC

Asked

Viewed 1,370 times

0

How to pick up the selected value on dropdownlist ? I need to take the selected value and pass this value to a session. I did with textbox this way and it worked, but with dropdownlist doesn’t take the value. I did the following:

View

  @Html.DisplayNameFor(model => model.IRPJ) : 
                    @Html.DropDownListFor(model => model.IRPJ, new SelectList(new List<Object> 
               {
                   new { value = 0, text = "1,5"},
                   new { value = 1, text = "4,8"}
               }, "value", "text", 0))

Controller

 [HttpPost]
        public ActionResult Detalhes(FormCollection frm)
        {
             object irpj = frm[1].ToString().Replace(".",",");
             Session["IRPJ"] = irpj;

            return RedirectToAction("ImprimirBoleto", new
            {
                irpj = Session["IRPJ"].ToString(),
            });
  • in this form, you only send this field?

  • @Rafaelcabral, No, send others, put only this pq was the only one I need that I didn’t know, the others are textbox and that way it worked.

  • then the @Rafaelmathias solution solves

1 answer

2


You do not need the Formcollection parameter in this case.

[HttpPost]
    public ActionResult Detalhes(string IRPJ)
    {

         Session["IRPJ"] = IRPJ.Replace(".",",");

        return RedirectToAction("ImprimirBoleto", new
        {
            irpj = Session["IRPJ"].ToString(),
        });
  • Don’t go man, pass 1 in the parameter.

  • 1

    passes 1, because it is value of it, the value that goes to view, put the value that Voce wants to receive in the "Value" that Voce created

  • @Rafaelcabral Ahhh understood, I’ll try.

  • @Rafaelcabral, it worked, thank you.

Browser other questions tagged

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