1

我的第一张桌子是:

第一个表名称:联系人
ContactID (PK)
FirstName
LastName
Company

第二个表名称:电话
ContactID (FK)
PhoneType
PhoneNumber

我的视图模型是

public class ContactVM2
{
    public int ContactID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Company { get; set; }

    public string PhoneType { get; set; }
    public string PhoneNumber { get; set; }
}

存储库类是

public class ContactRepository
{
    ContactsDBEntities dbRepo = new ContactsDBEntities();

    public List<ContactVM> GetAllContacts()
    {
        List<ContactVM> ContactViewList = new List<ContactVM>();

        var allContacts = dbRepo.Contacts.ToList();
        var allPhones = dbRepo.Phones.ToList();

        foreach (var cont in allContacts)
        {
            foreach (var ph in allPhones)
            {
                if (cont.ContactID == ph.ContactID)
                {
                    ContactViewList.Add(new ContactVM(){
                        ContactID =cont.ContactID,
                        FirstName=cont.FirstName,
                        LastName=cont.LastName,
                        Company=cont.Company,
                        PhoneType=ph.PhoneType,
                        PhoneNumber=ph.PhoneNumber});
                }
            }
        }

        return ContactViewList;
    }        
}

控制器是

public ActionResult Index()
    {
        ContactRepository contRepo = new ContactRepository();

        var allContacts = contRepo.GetAllContacts().ToList();

        return View(allContacts);
    }

我在联系人表中有以下数据
ContactID FirstName LastName Company
1 Bill Gates Microsoft

并在电话表中
ContactID PhoneType PhoneNumber
1 Home 1111
1 Office 2222

我得到以下结果
1 比尔盖茨家 1111
1 比尔盖茨办公室 2222

联系方式重复的地方。
我需要以下结果
1 Bill Gates Home 1111
                            Office 2222
我也尝试了以下视图中的更改

 <td style="border:2px solid Blue;">
        @{
        foreach (var parent in Model.Where(x=>x.ContactID==item.ContactID).GroupBy(m=>m.PhoneNumber))
       {   
          foreach( var itm in parent )
           {                
             @itm.PhoneNumber <br />
          }    
          } 
          }
    </td>
    <td style="border:2px solid red;">
        @{
        foreach (var parent in Model.Where(x=>x.ContactID==item.ContactID).GroupBy(m=>m.PhoneType))
       {   
          foreach( var itm in parent )
           {                
             @itm.PhoneType <br />
          }    
          } 
          }
    </td>

但它仍然再次重复记录。
然后我尝试在 ModelView 中进行以下更改

    public List<string> PhoneType { get; set; }
    public List<string> PhoneNumber { get; set; }

但没有得到结果。
任何人都可以通过给出最简单的例子来帮助我,因为我处于非常初学者的水平。 视图中没有电话迭代的代码

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.ContactID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.FirstName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.LastName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Company)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.PhoneType)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.PhoneNumber)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
        @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
        @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
    </td>
</tr>
4

1 回答 1

0

您应该从存储库类中删除迭代并为电话号码创建一个单独的类,并在 ContactVM2 中添加该类的变量,如下面的模型:

public class ContactVM2
{
    public int ContactID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Company { get; set; }
    public List<ContactVM2Phone> PhoneList {get; set;}

}
public class ContactVM2Phone
{
    public string PhoneType { get; set; }
    public string PhoneNumber { get; set; }
}

存储库类

public class ContactRepository
{
    ContactsDBEntities dbRepo = new ContactsDBEntities();

    public List<ContactVM> GetAllContacts()
    {
        List<ContactVM> ContactViewList = new List<ContactVM>();


        var allContacts = dbRepo.Contacts.ToList();
        var allPhones = dbRepo.Phones.ToList();

        foreach (var cont in allContacts)
        {
          ContactViewList obj=   new ContactVM()
           obj.ContactID =cont.ContactID;
           obj.FirstName=cont.FirstName;
           obj.LastName=cont.LastName;
           obj.Company=cont.Company;
           List<ContactVM2Phone> Phonelist= new List<ContactVM2Phone>();
            foreach (var ph in allPhones)
            {
                if (cont.ContactID == ph.ContactID)
                {
                    Phonelist.Add(new ContactVM2Phone(){
                        PhoneType=ph.PhoneType,
                        PhoneNumber=ph.PhoneNumber});
                }
            }
           obj.PhoneList =Phonelist;
           ContactViewList.Add(obj);
        }

        return ContactViewList;
    }        
}
于 2013-07-31T13:22:02.370 回答