Create a dropdownlist and get the selected id Asp.net MVC

Asked

Viewed 3,137 times

2

I need to create a Dropdownlist at runtime with 2 choices. With this, I need to do a check taking the selected record and then accessing such method. This after clicking on a ActionLink.

How to set an id for this dropdownlist to touch it? How can I do this?

So far I’ve done so:

<td>
    @Html.DropDownListFor(modelItem => item.EscolhaBanco, new SelectList(new List<Object>
    {
         new {value = 0, text = ""},
         new {value = 1, text = "Bradesco"},
         new {value = 2, text = "Santander"}
    }, "value", "text", 0))
</td>

 <td>@Html.ActionLink("Imprimir", "ImprimirBoleto", new { Fatura = item.NumeroDocumento }, new { target = "_blank" })</td>

1 answer

1


Set an anonymous object in the argument HtmlAttributes as follows:

@Html.DropDownListFor(modelItem => item.EscolhaBanco, new SelectList(new List<Object>
{
     new {value = 0, text = ""},
     new {value = 1, text = "Bradesco"},
     new {value = 2, text = "Santander"}
}, new { id = "IdDoMeuDropDownList" }))

EDIT

To pass the selected value to the Controller, you’ll have to use a <button> instead of a ActionLink. It would look like this:

@using (Html.BeginForm()) 
{
    <td>
        @Html.DropDownListFor(modelItem => item.EscolhaBanco, new SelectList(new List<Object>
    {
        new {value = 0, text = ""},
        new {value = 1, text = "Bradesco"},
        new {value = 2, text = "Santander"}
    }, "value", "text", 0))
    </td>

    <input type="submit" value="Enviar" class="btn btn-default" />
}

I don’t know if you’ve defined a Viewmodel for this, but the correct is to define the Viewmodel and make the View use it:

namespace MeuProjeto.ViewModels
{
    public class EscolhaBancoViewModel 
    {
        public int EscolhaBanco { get; set; }
    }
}

To View gets like this:

@model MeuProjeto.ViewModels.EscolhaBancoViewModel

@using (Html.BeginForm()) 
{
    <td>
        @Html.DropDownListFor(modelItem => item.EscolhaBanco, new SelectList(new List<Object>
    {
        new {value = 0, text = ""},
        new {value = 1, text = "Bradesco"},
        new {value = 2, text = "Santander"}
    }, "value", "text", 0))
    </td>

    <input type="submit" value="Enviar" class="btn btn-default" />
}

And the Controller:

public ActionResult MinhaAction(EscolhaBancoViewModel viewModel) 
{
    // O valor vai aparecer em viewModel.EscolhaBanco
}
  • and to get the Selecteditem or Index and do a check with it, you can do with the drop like this ?

  • @Andreeh What would you like to check?

  • I have 2 methods, one to return information from Radesco or santander, as the choice, with this, I need to make this check according to the choice of the drop and access the right method.

  • Yes. You intend to make one POST or will use Ajax?

  • This will only happen after clicking an Actionlink.

  • So it is POST. I will amplify the answer.

  • This, it’s post. Beauty.

  • @Andreeh See now.

  • actionlink in my code, is passing a parameter. And now ?

  • Update the question by putting this part in it.

  • Ready @Gypsy Morrison Mendez.

  • chat here is blocked. : S

  • @Andreeh No problem. Anything open another question I try to answer.

  • All right, thank you.

  • I rode a way here and it worked. I just need the wallet of the Boleto bank Bradesco. In dll I caught has not.

Show 10 more comments

Browser other questions tagged

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