Home > Software engineering >  MVC display a different view page according to dropdown list selected option
MVC display a different view page according to dropdown list selected option

Time:12-30

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())

}

CodePudding user response:

I solved it by using different action methods and added

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

inside the drop down list

  • Related