如何访问控制器中列表的元素

时间:2018-05-16 11:41:14

标签: asp.net-mvc

实体

public class Employee
{   
    public long BusinessUnitID{ get; set; }
    public long EmployeeID { get; set; }
    public long InfoTypeID { get; set; }
    public string EmployeeName { get; set; }
    public List<ContactData> ContactDetails{ get; set; }
}

public class ContactData
{
    public string ContactTypeName { get; set; }
    public string ContactValue { get; set; }
}

模型

Public class EmployeeDetails
{
    public long BusinessUnitID { get; set; }
    public List<EmployeeData> EmployeeInfo { get; set;}
    public List<ContactInfo> Contacts { get; set; }
}

public class EmployeeData
{
    public long EmployeeID { get; set;}
    public string EmployeeName { get; set;}
}

Public class ContactInfo
{
    public string ContactName { get; set; }
    public long ContactValue { get; set; }
}

控制器

public ActionResult Update(long BusinessUnitID=2)
{
    if (Session[Constants.Session_IsAdmin] != null && Convert.ToBoolean(Session[Constants.Session_IsAdmin]))

    {
        EmployeeDetails employeeDetails = new EmployeeDetails();
        List<Employee> employee = GetEmployeeById(Convert.ToInt64(BusinessUnitID));
        List<EmployeeData> lstEmployeeData = new List<EmployeeData>();
        List<ContactInfo> lstContactInfo = new List<OptionDetails>();        
        var ID = employee.Select(x => x.BusinessUnitID).ToList();
        foreach(var item in employee.Where(x => x.BusinessUnitID == BusinessUnitID))
        {
            EmployeeData employeeData = new EmployeeData();
            employeeData.EmployeeID = item.EmployeeID;
            employeeData.EmployeeName = item.EmployeeName;
            foreach (var local in employee.Where(q => q.EmployeeID == employeeData.EmployeeID))
            {
                //ContactInfo contactInfo = new ContactInfo();
                //contactInfo.ContactName = local.ContactDetails.Select(p => p.ContactName).ToString();
               //contactInfo.ContactValue = local.ContactDetails.Select(s => s.ContactValue).ToString();
            }
            lstEmployeeData.Add(employeeData);
        }
        return View(EmployeeDetails);
    }
    else
    {
        return RedirectToAction("Login");
    }
}

我在这里获得了一个列表Employee,其中我有以下属性和一个列表ContactDetails,这是一个包含至少3个元素属性的列表。例如3种类型的ContactTypeNameContactValue作为Home: 000000000, work: 9999999, mobile: 8888888。对于businessUnitid,我得到了所有employeeid的特定EmployeeID我想要联系方式,但我无法获得或3 contactvaluecontactname。列表Employee中有列表ContactDetails,其中有3个或5个联系号码。我不知道如何将其分配到列表中。

1 个答案:

答案 0 :(得分:0)

由于员工可以拥有多个联系人详细信息,因此您需要将联系人详细信息列表属性添加到EmployeeData类

模型类:

Public class EmployeeDetails
{
    public EmployeeDetails()
    {
        EmployeeInfo = new List<EmployeeData>();
    }
    public long BusinessUnitID { get; set; }
    public List<EmployeeData> EmployeeInfo { get; set;}
}

public class EmployeeData
{
    public EmployeeData()
    {
        Contacts = new List<ContactInfo>();
    }
    public long EmployeeID { get; set;}
    public string EmployeeName { get; set;}
    public List<ContactInfo> Contacts { get; set; }
}

Public class ContactInfo
{
    public string ContactName { get; set; }
    public long ContactValue { get; set; }
}

然后很容易传递具有多个联系人的员工数据

public ActionResult Update(long BusinessUnitID=2)
{
    if (Session[Constants.Session_IsAdmin] != null && Convert.ToBoolean(Session[Constants.Session_IsAdmin]))

    {
        List<Employee> employees = GetEmployeeById(Convert.ToInt64(BusinessUnitID));

        List<EmployeeData> lstEmployeeData = new List<EmployeeData>();        

        foreach(var item in employee.Where(x => x.BusinessUnitID == BusinessUnitID))
        {
            EmployeeData employeeData = new EmployeeData();
            employeeData.EmployeeID = item.EmployeeID;
            employeeData.EmployeeName = item.EmployeeName;
            foreach (var contact in employee.ContactDetails)
            {
                ContactInfo contactInfo = new ContactInfo();
                contactInfo.ContactName = contact.ContactName;
                contactInfo.ContactValue = contact.ContactValue;
                employeeData.Contacts.Add(contactInfo);
            }

            lstEmployeeData.Add(employeeData);
        }
        EmployeeDetails empDetails = new EmployeeDetails();
        empDetails.EmployeeInfo = lstEmployeeData;
        return View(empDetails);
    }
    else
    {
        return RedirectToAction("Login");
    }
}