0

I'm working on an ASP.NET Core MVC application, and I'm facing an issue with dropdown lists. Specifically, I have dropdown lists for "Company" and "Department" in an Edit view, and I'm trying to display the selected values when rendering the view. However, the selected values are not displaying as expected.

Here's an overview of my code:

In my controller, I have an Edit action method that retrieves user data and populates the dropdown lists with the appropriate values. The selected values are also set based on the user's data.

 [HttpGet] public async Task<IActionResult> Edit(string fullname) { User UserModel = await _authservice.GetByNameAsync(fullname); var roles = _roleManager.Roles.Select(r => new SelectListItem { Value = r.Name, Text = r.Name }).ToList(); // Retrieve a list of companies to populate the dropdown list var companies = await _dbcontext.Company .Select(company => new SelectListItem { Value = company.Id.ToString(), Text = company.CompanyName, }) .ToListAsync(); var departmentsWithCompany = _dbcontext.DepartmentComp .Join(_dbcontext.Company, department => department.CompId, company => company.Id, (department, company) => new SelectListItem { Value = department.Id.ToString(), Text = $"{department.DepartmentName} - {company.CompanyName}" }) .ToList(); var Registration = new RegistrationModel() { Name = UserModel.Name, StaffId = UserModel.StaffId ?? 0, Username = UserModel.UserName, Email = UserModel.Email, CompanyName = UserModel.Company, ContactNumber = UserModel.ContactNumber, Department = UserModel.Department, RoleList = roles, Role = string.Join(",", userManager.GetRolesAsync(UserModel).Result.ToArray()), // Set the selected company and department based on user data SelectedCompId = UserModel.CompId ?? 0, SelectedDepartmentId = UserModel.DeptId ?? 0, CompanyList = companies, DepartmentList = departmentsWithCompany }; return View(Registration); } } [HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> EditAsync(RegistrationModel model) { // ... // Updating user data and retrieving result // ... // Repopulating dropdown lists in model return View(model); } 

In my Edit.cshtml view, I have the following code to display the dropdown lists:

<div class="form-group"> <label asp-for="SelectedCompId" class="control-label"></label> <select asp-for="SelectedCompId" asp-items="Model.CompanyList" class="form-control"></select> <span asp-validation-for="SelectedCompId" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="SelectedDepartmentId" class="control-label"></label> <select asp-for="SelectedDepartmentId" asp-items="Model.DepartmentList" class="form-control"></select> <span asp-validation-for="SelectedDepartmentId" class="text-danger"></span> </div> 

The issue is that the selected values for "Company" and "Department" are not displayed in the dropdown lists when I render the view. Other form fields like "Name" and "Role" work as expected.

I suspect the issue might be in how I'm setting the SelectedCompId and SelectedDepartmentId properties in my controller's Edit action and EditAsync action. Could you please help me understand what might be causing this issue and how I can ensure that the selected values are displayed correctly in the dropdown lists?

Thank you!

3
  • You have to set the Selected property of the SelectListItem to true in your post methodCommentedSep 26, 2023 at 1:46
  • @RuikaiFeng Thank you for your input, but I found the solution by passing it by Name rather than Id. It also works. Thank you in any way
    – wana isa
    CommentedSep 27, 2023 at 4:13
  • Does this answer your question? Select Tag Helper in ASP.NET Core MVCCommentedOct 5, 2023 at 6:50

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.