Я создал приложение ASP.NET MVC 5, подключенное к базе данных, и оно прекрасно работает, за исключением возможности фильтровать один раскрывающийся список на основе выбора в другом раскрывающемся списке.
Я попробовал скрипт, который перенаправляет на
Код: Выделить всё
ActionResult
Can someone provide guidance on making this populate the
Код: Выделить всё
Defect_id
Код: Выделить всё
Production_area_assigned_id
This is the script including the commented redirect attempt:
Код: Выделить всё
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;
}
Код: Выделить всё
Create()
Код: Выделить всё
Defect_id
Код: Выделить всё
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
Код: Выделить всё
@Html.LabelFor(model => model.Production_area_assigned_id, "Production_area_assigned_id", htmlAttributes: new { @class = "control-label col-md-2" })
@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" })
@* This needs to be a partial view (render works) and feed the id selected above *@
@{ Html.RenderPartial("_DefectDropdown"); }
Источник: https://stackoverflow.com/questions/781 ... -selection