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
@Andreeh What would you like to check?
– Leonel Sanches da Silva
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.
– AndreeH
Yes. You intend to make one
POST
or will use Ajax?– Leonel Sanches da Silva
This will only happen after clicking an Actionlink.
– AndreeH
So it is
POST
. I will amplify the answer.– Leonel Sanches da Silva
This, it’s post. Beauty.
– AndreeH
@Andreeh See now.
– Leonel Sanches da Silva
actionlink in my code, is passing a parameter. And now ?
– AndreeH
Update the question by putting this part in it.
– Leonel Sanches da Silva
Ready @Gypsy Morrison Mendez.
– AndreeH
chat here is blocked. : S
– AndreeH
@Andreeh No problem. Anything open another question I try to answer.
– Leonel Sanches da Silva
All right, thank you.
– AndreeH
I rode a way here and it worked. I just need the wallet of the Boleto bank Bradesco. In dll I caught has not.
– AndreeH