0

How can I bind specific view pages to selected dropdown list option?

This is the dropdown list inside my main view.

@using(Html.BeginForm("selectFrom", "Company", FormMethod.Get)) 
                {  
@Html.DropDownList("CATEGORIES",
                 new List<SelectListItem> 
                    {
                         new SelectListItem {
                            Text="COMPONENT",
                            Value="COMPONENT",
                        
                       
                         },
                         new SelectListItem {
                            Text="SOFTWARE",
                            Value="SOFTWARE",
                         },
                         new SelectListItem {
                                                Text="SERVICE",
                                                Value="SERVICE",
                                             }
           
                    })
                    <input type="submit" value="Submit" />
                 }

Is it possible to do something like this inside the same action result?:

       [HttpPost]
        public ActionResult selectFrom(string CATEGORIES)
        {
            if (CATEGORIES=="COMPONENT")
            {
                                //return View(_context.COMBINED.Where(x => x.Laptop != " ").ToList());

            }
          if (CATEGORIES=="SOFTWARE")
          {
           .......
          }
        }

or inside different action results: `

public IActionResult COMPONENT(){
return View(_context.COMBINED.Where(x => x.Laptop != " " || x._2_In_1 != "").ToList())

}

public IActionResult SOFTWARE(){
return View(_context.COMBINED.Where(x => x.Laptop != " " ).ToList())

}
E__
  • 45
  • 8
  • Does this answer your question? [Invoking particular action on dropdown list selection in MVC](https://stackoverflow.com/questions/2213055/invoking-particular-action-on-dropdown-list-selection-in-mvc) – Jackdaw Dec 27 '22 at 13:17
  • @Jackdaw I tried the accepted answer and now I can redirect to only one page for every time that selected value changed. But not according to selected values. Also top answer, but I couldn't understand where to define function. – E__ Dec 27 '22 at 13:56
  • 1
    thanks now I see, `new {onchange = "window.location.href='/Company/' + this.value;"}` – E__ Dec 27 '22 at 14:14

1 Answers1

0

I solved it by using different action methods and added

new {onchange = "window.location.href='/Company/' + this.value;"}

inside the drop down list

E__
  • 45
  • 8