kuromi suluk
kuromi suluk
CC#
Created by kuromi suluk on 4/1/2024 in #help
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.
8 replies