如何在ASP.NET Controller中访问List元素并映射员工联系信息
Got it, let's get that contact info mapping working correctly. The main issues in your current code are incorrect list initialization, redundant queries, and not properly iterating over each contact detail entry. Here's the fixed version of your Update method, followed by a breakdown of the key changes:
Corrected Controller Code
public ActionResult Update(long BusinessUnitID = 2) { if (Session[Constants.Session_IsAdmin] != null && Convert.ToBoolean(Session[Constants.Session_IsAdmin])) { EmployeeDetails employeeDetails = new EmployeeDetails(); List<Employee> employees = GetEmployeeById(Convert.ToInt64(BusinessUnitID)); // Fix 1: Initialize contact list with correct type List<EmployeeData> lstEmployeeData = new List<EmployeeData>(); List<ContactInfo> lstContactInfo = new List<ContactInfo>(); employeeDetails.BusinessUnitID = BusinessUnitID; foreach (var employee in employees.Where(x => x.BusinessUnitID == BusinessUnitID)) { // Map employee basic info EmployeeData employeeData = new EmployeeData(); employeeData.EmployeeID = employee.EmployeeID; employeeData.EmployeeName = employee.EmployeeName; lstEmployeeData.Add(employeeData); // Fix 2: Directly iterate over the employee's ContactDetails list (no redundant query) foreach (var contact in employee.ContactDetails) { ContactInfo contactInfo = new ContactInfo(); contactInfo.ContactName = contact.ContactTypeName; // Map ContactTypeName to ContactName // Fix 3: Handle type conversion (string to long) - add error handling if needed if (long.TryParse(contact.ContactValue, out long contactValue)) { contactInfo.ContactValue = contactValue; } else { // Handle invalid contact value (e.g., set to 0 or throw an error) contactInfo.ContactValue = 0; } lstContactInfo.Add(contactInfo); } } // Fix 4: Assign populated lists to the employeeDetails object employeeDetails.EmployeeInfo = lstEmployeeData; employeeDetails.Contacts = lstContactInfo; // Fix 5: Return the instance, not the class name return View(employeeDetails); } else { return RedirectToAction("Login"); } }
Key Changes Explained
- Fixed list initialization: Changed
List<OptionDetails>toList<ContactInfo>for the contact list—your original code had a type mismatch here that would cause compilation errors. - Removed redundant query: Instead of re-querying the employees list for the same employee ID, we directly use
employee.ContactDetailssince we already have the current employee object in the loop. This is more efficient and cleaner. - Proper contact mapping: Iterate over each
ContactDataentry in the employee'sContactDetailslist, mapping each field correctly to a newContactInfoobject. Your original code tried to select all contacts at once, which wouldn't create individual entries. - Handled type conversion: Since
ContactData.ContactValueis a string butContactInfo.ContactValueis a long, we uselong.TryParseto safely convert the value. Add custom error handling here if you need to log invalid entries or notify the user. - Populated the view model: Assigned the
lstEmployeeDataandlstContactInfoto theemployeeDetailsobject so the view receives the fully populated data. - Fixed view return: Changed
return View(EmployeeDetails);toreturn View(employeeDetails);to pass the actual instance of the model instead of the class type, which would have caused a runtime error.
Note: If ContactInfo.ContactValue was supposed to be a string (maybe a typo in your model), you can skip the conversion and just assign contactInfo.ContactValue = contact.ContactValue;—but you'll need to update the ContactInfo class's property type first to match.
内容的提问来源于stack exchange,提问作者renu pawar
相关产品推荐
相关产品推荐

