C
C#11mo ago
kuromi suluk

How to display only department fields associated with a selected department in student automation

hi, I'm facing a challenge while working on the student automation system. After selecting the department field in the form, I want to display only the departments associated with the selected unit in the department field. Currently, I can see that the units are listed correctly in the unit selection dropdown, but all departments are listed in the department selection dropdown and not filtered. I'm struggling to find a solution for this. How can I display only the departments associated with the selected unit in the department field after selecting a unit? Do I need to edit my existing code or develop a new approach for this? Controller.cs:
c#
[HttpGet]
public IActionResult AddStudent()
{
ViewBag.departments = _context.departments.ToList();
ViewBag.units = _context.units.ToList();
ViewBag.cities = _context.cities.ToList();

return View();
}
c#
[HttpGet]
public IActionResult AddStudent()
{
ViewBag.departments = _context.departments.ToList();
ViewBag.units = _context.units.ToList();
ViewBag.cities = _context.cities.ToList();

return View();
}
AddStudent.cshtml:
c#
@model Student
<form asp-action="AddStudent" method="post" class="m-5">
<div>
<select class="form-control" asp-for="Department.Unit.unitId" asp-items="@(new SelectList(ViewBag.units, "unitId", "unitName"))" required="true">
<option value="">Pick Unit</option>
</select>
</div>

<div>
<select class="form-control" asp-for="unitId" asp-items="@(new SelectList(ViewBag.units, "departmentId", "departmentName"))" required="true">
<option value="">Pick Department</option>
</select>
</div>

<div>
<select class="form-control" asp-for="cityId" asp-items="@(new SelectList(ViewBag.cities, "cityId", "cityName"))" required="true">
<option value="">Pick City</option>
</select>
</div>

<button type="submit" class="btn btn-primary">Save</button>

</form>
c#
@model Student
<form asp-action="AddStudent" method="post" class="m-5">
<div>
<select class="form-control" asp-for="Department.Unit.unitId" asp-items="@(new SelectList(ViewBag.units, "unitId", "unitName"))" required="true">
<option value="">Pick Unit</option>
</select>
</div>

<div>
<select class="form-control" asp-for="unitId" asp-items="@(new SelectList(ViewBag.units, "departmentId", "departmentName"))" required="true">
<option value="">Pick Department</option>
</select>
</div>

<div>
<select class="form-control" asp-for="cityId" asp-items="@(new SelectList(ViewBag.cities, "cityId", "cityName"))" required="true">
<option value="">Pick City</option>
</select>
</div>

<button type="submit" class="btn btn-primary">Save</button>

</form>
Thank you.
2 Replies
Angius
Angius11mo ago
Start by not using the ViewBag Your view declares that the @model for it will be of type Student Yet you don't pass any model to the View() method Far as selecting the data you want, in the shape you need... I assume you're using EF Core here If so, then utilize LINQ Don't just dump the entire database
kuromi suluk
kuromi sulukOP11mo ago
tnx

Did you find this page helpful?