11
I’m trying to do a search, filtering through a dropdowlist. In short, I select a level of instruction in a list and when I click 'Search' I want to return to the list of people who have that level of education. My problem is the identification of the selected item in the dropdownlist. If I select anything the search does not return me anything. But if I pass a value to the id as in the example below, when I click on Search I get back all the people who have that level equal to the informed id. What 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 Pesquisa
<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>
let me get this straight. I create a Partial call _Search that can be used on multiple screens. Ok. Create viewModel ok. Item 4, the controller you refer to is what I create to do some research right? And in item 5, 'Search.cshtml is a reference to the previously created controller.
– Victor
If I create 'Searchviewmodel' as a model, instead of a viewModel folder it makes a difference?
– Victor
On the 4, yes, it is the Controller which will receive the survey. Item 5 is the outworking research. You can even call
ResultadoPesquisa.cshtml
, if you want.– Leonel Sanches da Silva
Better you separate Viewmodel of Model. Viewmodels are not bank mapped.
– Leonel Sanches da Silva
Do I have to reference then in the controllers? 'using Minhabase.ViewModel.Pesquisaviewmodel' ?
– Victor
Surely you must.
– Leonel Sanches da Silva