0

I am trying to order my Html.List box based on a selected value from another Selected Dropdown but don't know how to do it

This is my Html.ListBox filled by a ViewBag o my VIEW

@Html.ListBox("Group", (IEnumerable<SelectListItem>)ViewBag.Group, new { style = "width: 300px;" })

This is my View Dropdown list:

<select id="sel2">
    <option value="">Todos</option>
    @foreach (var item in Model.Select(l => l.Rid).Distinct())
    {
        <option value="@item">@item</option>
    }
</select>

And this is my ViewBag in the controller:

List<string> listadepuntos = new List<string>();
foreach (var item in db.Pos.Where(r => r.Fecha.Day <= today.Day)
    .Select(g => new { Pdv = g.Pdv, Total = g.Total })
    .GroupBy(l => l.Pdv).AsEnumerable().Select(z => new
    {
        Punto_De_Venta=z.Key, 
        Total = String.Format("{0:$#,##0.00;($#,##0.00);Zero}",Decimal.Round(z.Sum(l => l.Total), 0)) 
    }))
{
    listadepuntos.Add(item.ToString());
}
var grupoPdv = new SelectList(listadepuntos.ToList());
ViewBag.GroupS = grupoPdv;

Is there a way to order my Viewbag items from a Selected option in my dropdown? maybe using Javascript or something? i have no idea how to approach the problem

tereško
  • 58,060
  • 25
  • 98
  • 150
Arturo Martinez
  • 389
  • 7
  • 28
  • Do you want to sort the values in Viewbag before adding it to the List ? If that's so then why not call sort on the listadepuntos itself? – Rohit Mar 08 '16 at 04:38
  • well i have been reading and it seems i cant sort them at the same time a selected value is selected since they come from server side, so i woul like to sort them before loading the view ow can i do it? – Arturo Martinez Mar 08 '16 at 04:40
  • why not sort the `listadepuntos` itself? – Rohit Mar 08 '16 at 04:41
  • You can use [this answer](http://stackoverflow.com/questions/667010/sorting-dropdown-list-using-javascript/667198#667198). Just modify sort function based on whatever your selection was elsewhere on the page. – Sergei Z Mar 08 '16 at 06:22

0 Answers0