You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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> to List<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.ContactDetails since we already have the current employee object in the loop. This is more efficient and cleaner.
  • Proper contact mapping: Iterate over each ContactData entry in the employee's ContactDetails list, mapping each field correctly to a new ContactInfo object. Your original code tried to select all contacts at once, which wouldn't create individual entries.
  • Handled type conversion: Since ContactData.ContactValue is a string but ContactInfo.ContactValue is a long, we use long.TryParse to 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 lstEmployeeData and lstContactInfo to the employeeDetails object so the view receives the fully populated data.
  • Fixed view return: Changed return View(EmployeeDetails); to return 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:23:45