In my application I have a DropDownList
that filters the search data. When filtering, it retrieves the value of QueryString
and leaves the value saved in DropDownList
itself (so that when the page changes, the filter option continues). However, I changed the route in my RouteConfig.cs
, to show the most friendly routes.
Before changing the route, I had this URl
:
~ / Index? year = 2015 & branch = 1 & typePresition = 2
I use Request[" "]
in each DropDownList
, thus retrieving the value.
When changing the route, my URL
changed to:
~ / Index / 2015/1 / = 2
However, Request[" "]
can not retrieve the value, thus returning DropDownList
always to the first value.
My DropDownList
is as follows:
<div class="col-md-2">
Tipo Prestação de Contas:
@Html.DropDownList("ddlPrestacao", new SelectList(ViewBag.TipoPrestacao, "TipoPrestacaoId", "Descricao", Request["tipoPrestacao"]), new { @class = "form-control" })
</div>
<div class="col-md-2">
Ano:
@Html.DropDownList("ddlAno", new SelectList(ViewBag.Ano, "anoVigencia", "anoVigencia", Request["ano"]), new { @class = "form-control" })
</div>
<div class="col-md-2">
Filial:
@Html.DropDownList("ddlFilial", new SelectList(ViewBag.Filiais, "value", "text", Request["filial"]), new { @class = "form-control" })
</div>
My route has been changed to:
//Antes
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Cliente", action = "Index", id = UrlParameter.Optional }
);
//Depois
routes.MapRoute(
name: "ListaClientes",
url: "{controller}/{action}/{ano}/{filial}/{tipoPrestacao}",
defaults: new { controller = "Cliente", action = "Index", ano = "", filial = "", tipoPrestacao = "" }
);
Edit
My Controller
is as follows:
[Authorize]
public ActionResult Index(string ano, string filial, string tipoPrestacao)
{
var tipoPrestacoes = new List<SelectListItem>();
tipoPrestacoes.Add(new SelectListItem { Text = "Mensal", Value = "1" });
tipoPrestacoes.Add(new SelectListItem { Text = "Quadrimestral", Value = "2" });
tipoPrestacoes.Add(new SelectListItem { Text = "Mensal + M13 M14", Value = "3" });
ViewBag.Tipo = tipoPrestacoes;
var anoPrestacao = _clienteRepository.ObterTodos()
.GroupBy(c => c.AnoVigencia)
.Select(c => c.FirstOrDefault())
.Select(items => new ClienteViewModel() { ClienteId = items.ClienteId, AnoVigencia = items.AnoVigencia }).ToList();
ViewBag.Ano = anoPrestacao;
var prestacaoTipo = _pprestacaoRepository.ObterTodos()
.GroupBy(c => c.Descricao)
.Select(c => c.FirstOrDefault())
.Select(items => new TipoPrestacaoViewModel() { TipoPrestacaoId = items.TipoPrestacaoId, Descricao = items.Descricao }).ToList();
ViewBag.TipoPrestacao = prestacaoTipo;
return View(_clienteRepository.ObterTodos());
}