Hello everyone this is my 1st post on here so be gentle if I say something or do something dumb :P Also this is my 1st real project in ASP.NET and I am sure that I made some mistake in my codes.
I am working on an ASP.NET MVC 5 web app that has 3 DropDownLists that are populated with strings from a database. The 3 lists are Semester, Course, and Consultant name. I want to be able to dynamically change the DropDownLists based on the selections made.
What I currently have done assumes that the semester is picked 1st followed by course then consultant name. The code doesn't populate the consultants name's when after picking semester.
After I understand my mistakes I will also want to have it where any of the 3 options can be chosen in what ever order.
this is my controller
public ActionResult Index(string Semester1, string Course1, string Consultant1)
{
ViewBag.semester = new SelectList(db.StudentInfos.Select(x => x.semester).Distinct().OrderBy(x => x));
ViewBag.course = new SelectList(db.StudentInfos.Select(x => x.WCOnlineCourse) .Distinct().OrderBy(x => x));
ViewBag.consultant = new SelectList(db.StudentInfos.Select(x => x.consultant).Distinct().OrderBy(x => x));
if (Semester1 != null)
{
ViewBag.course = new SelectList(db.StudentInfos.Where(x => x.semester == Semester1).Select(x => x.WCOnlineCourse).Distinct().OrderBy(x => x));
ViewBag.consultant = new SelectList(db.StudentInfos.Where(x => x.semester == Semester1).Select(x => x.consultant).Distinct().OrderBy(x => x));
if (Course1 != null)
{
ViewBag.consultant = new SelectList(db.StudentInfos.Where(x => x.semester == Semester1).Where(x => x.WCOnlineCourse == Course1).Select(x => x.consultant).Distinct().OrderBy(x => x));
}
}
return View();
}
this is my view
@model IEnumerable<StudentSurvey.Models.StudentInfo>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table class="table">
<tr>
<th>
Semester
</th>
<th>
Course
</th>
<th>
Consultant
</th>
</tr>
<tr>
@using (Html.BeginForm("Index", "StudentInfos", FormMethod.Post))
{
<td>
@Html.DropDownList("Semester1", (SelectList)ViewBag.semester, "Select Semester", new { onchange = "this.form.submit();" })
</td>
<td>
@Html.DropDownList("Course1", (SelectList)ViewBag.course, "Select Course", new { onchange = "this.form.submit();" })
</td>
<td>
@Html.DropDownList("Consultant1", (SelectList)ViewBag.consultant, "Select Consultant", new { onchange = "this.form.submit();" })
</td>
}
</tr>
</table>