Get URL value from a Dropdownlist

Asked

Viewed 338 times

2

In my application I own a DropDownList that filters the search data. When filtering, it retrieves the value of the QueryString and save the value in the DropDownList (so that when changing page, the filter option continues). However, I changed the route in my RouteConfig.cs, to show the friendliest routes.

Before I changed course, I owned this URl:

~/Index? ano=2015&filial=1&tipPrestacao=2

I use the Request[" "] in each DropDownList, thus recovering the value.

By changing course, my URL moved to:

~/Index/2015/1/=2

However, in this way the Request[" "] cannot recover the value, thus returning the DropDownList always to the first value.

Man 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

Man 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());
        }
  • You can also put in your question how is the Action of Controller?

  • @I edited the answer with Action.

1 answer

2


It’s all right. Just use RouteData.Values instead of Request:

<div class="col-md-2">
    Tipo Prestação de Contas:
    @Html.DropDownList("ddlPrestacao", new SelectList(ViewBag.TipoPrestacao, "TipoPrestacaoId", "Descricao", ViewContext.RouteData.Values["tipoPrestacao"]), new { @class = "form-control" })
</div>
<div class="col-md-2">
    Ano:
    @Html.DropDownList("ddlAno", new SelectList(ViewBag.Ano, "anoVigencia", "anoVigencia", ViewContext.RouteData.Values["ano"]), new { @class = "form-control" })
</div>
<div class="col-md-2">
    Filial:
    @Html.DropDownList("ddlFilial", new SelectList(ViewBag.Filiais, "value", "text", ViewContext.RouteData.Values["filial"]), new { @class = "form-control" })
</div>
  • 1

    That’s right. Thanks for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.