Partial view dropdown contents filtered by other dropdown selection

33 Views Asked by At

I have created a database-connected ASP.NET MVC 5 application and it works beautifully with the exception of being able to filter one dropdown based in the selection in another dropdown.

I have tried script which redirects to an ActionResult in the controller but I couldn't get back to the calling screen with state intact. I have tried to include the filtering code in that script but it seems to never be called.

Can someone provide guidance on making this populate the Defect_id dropdown based on the selection from Production_area_assigned_id?

This is the script including the commented redirect attempt:

<script>
    function GetDefects(val) {
        //window.location.href = "GetDefects/" + val;
        //can I move the code in here instead of redirect?
        var dbResult = db.Defect_info.Where(d => d.Production_area_id == val).ToList();
        //var defects = (from d in dbResult
        //       select new
        //    {
        //        d.Defect_id,
        //        d.Defect_name
        //    });
        ViewBag.DefectList = dbResult.ToList();
        //string empid = Request.Cookies.AllKeys.Contains("userid") ? Request.Cookies["userid"].Value : "??";
        //DataTable dtt = new DataTable();
        //con.Open();
        //SqlCommand cmd = new SqlCommand("Select Production_area_id from Employee where Employee_id = " + empid, con);
        //cmd.CommandType = CommandType.Text;
        //SqlDataAdapter adp = new SqlDataAdapter(cmd);
        //adp.Fill(dtt);
        //con.Close();
        //ViewBag.userid = Request.Cookies.AllKeys.Contains("userid") ? Request.Cookies["userid"].Value : "??";
        //ViewBag.Defect_id = ViewBag.DefectList;
    }
</script>

Create() within the controller is attempting to populate all data or only Defect_id dropdown

public ActionResult Create()
{
     string empid = Request.Cookies.AllKeys.Contains("userid") ? Request.Cookies["userid"].Value : "??";
     DataTable dtt = new DataTable();

     con.Open();

     SqlCommand cmd = new SqlCommand("Select Production_area_id from Employee where Employee_id = " + empid, con);
     cmd.CommandType = CommandType.Text;

     SqlDataAdapter adp = new SqlDataAdapter(cmd);
     adp.Fill(dtt);
     con.Close();

     // GetDefects(9); // This works, how do I feed onchange Production_area_assigned_id?
     //int prodid = Convert.ToInt16(dtt.Rows[0]["Production_area_id"].ToString());
     //GetDefects(prodid);
     if (ViewBag.Production_area_assigned_id == null)
     {
         ViewBag.userid = Request.Cookies.AllKeys.Contains("userid") ? Request.Cookies["userid"].Value : "??";
         ViewBag.Defect_id = new SelectList(db.Defect_info, "Defect_id", "Defect_name");
         ViewBag.Employee_id = new SelectList(db.Employees, "Employee_id", "Employee_fullname", ViewBag.userid);
         ViewBag.Production_area_id = new SelectList(db.Production_area, "Production_area_id", "Production_area_name", ViewBag.prodid);
         ViewBag.Production_area_assigned_id = new SelectList(db.Production_area, "Production_area_id", "Production_area_name");
     } 
     else
     {
         ViewBag.Defect_id = ViewBag.DefectList;
     }

     //ViewBag.Employee_id[ViewBag.userid].Selected = true;
     return View();
}

Create.cshtml:

<div class="form-group">
    @Html.LabelFor(model => model.Production_area_assigned_id, "Production_area_assigned_id", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("Production_area_assigned_id", null, htmlAttributes: new { @class = "form-control", @onchange = "GetDefects(this.value)" })
        @Html.ValidationMessageFor(model => model.Production_area_assigned_id, "", new { @class = "text-danger" })
    </div>
</div>

@* This needs to be a partial view (render works) and feed the id selected above *@
<div>
    @{ Html.RenderPartial("_DefectDropdown"); }
</div>
0

There are 0 best solutions below