I'm trying to do a search by filtering through a dropdowlist. In summary, I select a level of instruction in a list and clicking on 'Search' I want to return the list of people who have that level of education. My problem is in identifying the item selected in the dropdownlist. If I select anything the search does not return anything to me. But if I pass a value to the id like in the example below, clicking on Search I get as a return all people who have that level equal to the id informed. What do I need to do to get the id of the selected item?
My Controller
public class FormacaoController : Controller
{
private DataContext db = new DataContext();
public ActionResult Index()
{
ViewBag.NívelId = new SelectList(db.Escolaridades, "Id", "Nome");
return View();
}
[HttpPost]
public ActionResult Pesquisa(int id = 3) // 'setando' um valor, ao clicar em buscar, retorna as informaçãos. Se não passar um valor aqui para o id, a consulta não retorna nada.
{
var resultado = db.Cursos.Include(c => c.Perfil).Where(c => c.NívelId == id);
ViewBag.Perfil = db.Perfis.AsEnumerable();
ViewBag.NívelId = new SelectList(db.Escolaridades, "Id", "Nome");
return View(resultado);
}
}
View Index
<div>
@using (Html.BeginForm("Pesquisa", "Formacao", FormMethod.Post))
{
IEnumerable<MinhaBase.Models.Perfil> modelPerfil = (IEnumerable<MinhaBase.Models.Perfil>)ViewBag.Perfil;
<div class="form-group">
@Html.DropDownList("NívelId")
</div>
<button type="submit" id="pesquisar" class="btn btn-primary btn-xs">Buscar</button>
}
View Search
<div>
@using (Html.BeginForm("Pesquisa", "Formacao"))
{
IEnumerable<MinhaBase.Models.Perfil> modelPerfil = (IEnumerable<MinhaBase.Models.Perfil>)ViewBag.Perfil;
<div class="form-group">
@Html.DropDownList("NívelId")
</div>
<button type="submit" id="pesquisar" class="btn btn-primary btn-xs">Buscar</button>
}
<table>
@{
if (Model != null)
{
foreach (var item in Model)
{
<tr>
<th>Nome <b> </b></th>
</tr>
<tr>
<td width="30%">@item.Nome></td>
<td width="30%">@item.Instituição</td>
<td width="30%">@item.Perfil.Nome</td>
</tr>
}
}
}
</table>