How to pass parameter to MVC controller

4

How to pass text or index from a DropDownList to Controller at the press of a button?

I tried using @model and could not. I tried by adding [HttpPost] to Controller and using the submit button and it did not work.

Controller :

public ActionResult Pesquisar(string _ddl)
{
  string opcaoDdl = _ddl;
  return View();
}

Index :

 @Html.DropDownList("ddlListaUnidades", new SelectList(ViewBag.listaUnidades), new { @class = "dllListaUnidades" })

<div class="botoesfiltrodiv">

    <input class="filtro-buttons" type="button" value="Limpar" />
    <input class="filtro-buttons" type="button" value="Pesquisar" onclick="location.href='@Url.Action("Pesquisar", "UnidadeAtendimento")'" />

</div>

When I press the button, it even calls controller , but I can not pass the parameter.

    
asked by anonymous 19.01.2017 / 19:11

1 answer

7

> html generated in the Controller case:

[HttpPost()]
[ValidateAntiForgeryToken()]
public ActionResult Pesquisar(string ddlListaUnidades)
{    
    string opcaoDdl = ddlListaUnidades;    

    return View();
}

and in the tag part you have to use this way:

@using (Html.BeginForm("Pesquisar","UnidadeAtendimento", FormMethod.Post)) 
{
    @Html.AntiForgeryToken()
    @Html.DropDownList("ddlListaUnidades", new SelectList(ViewBag.listaUnidades),
                   new { @class = "dllListaUnidades" })    
    <div class="botoesfiltrodiv">    
        <input class="filtro-buttons" type="button" value="Limpar" />
        <input class="filtro-buttons" type="submit" value="Pesquisar" />    
    </div>

}

19.01.2017 / 19:14