C
C#3mo ago
hığ

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
Angius3mo 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
hığ
hığ3mo ago
tnx
Want results from more Discord servers?
Add your server
More Posts
Help with DiscordGameSDK in a C# project (no Unity)I am currently trying to develop a plugin for a Unity game. For this I created a C# project, where IMarshal.GetActiveObject("Word.Application") fall in exceptionI'm working on a `.NET Framework` console application, I have added the `Microsoft.Office.Interop.WMigrating to Microsoft.IdentityModel.JsonWebTokens .net6 => .net8Hi, I am migrating a project which uses Duende as identity server + Ocelot gateway from .NET 6 to .NProcess working set is twice as commited managed memoryHi! I have an ASP.NET Core 8 app running on Linux in Docker. It's memory usage is limited by 512M. Resource based authorization with IdentityHello, I have the following setup in my asp.net core project: A `Workspace` model that has a bunch o✅ is .net 8.0 is not compatibleHey hi guys I'm new to this group and .net I just started learning .net I don't have any experience Help with why code is returning this way.Hello, I'm a CS student learning C# for the first time and i'm having some confusion on why my code Named pipe connection over network error (The username or password is incorrect)When running a solution where one machine runs a server and a second machine runs a client connectinDifference algorithm that Git usesHello! Quick rundown: I have a need to detect changes in custom bytecode sequences. Essentially, I Do I add my Models in the IdentityDbContext from the template?I'm trying to create a project, where I'll have two types of users (regular and admin) and I'll have