View MVC EF6中的重复行

时间:2014-04-11 18:38:56

标签: c# asp.net-mvc entity-framework

我在视图中收到重复的行。我想要一个显示用户所有数据的视图(电子邮件,电话,地址)。当我点击具有两个不同地址,电话号码和电子邮件的用户时,我的列表中最终会有8个内容,因此我的viewmodel中有8个对象。

以下是我查询数据库的方法。

public ActionResult View_All(int? id)
        {
            var cape = from c in db.Constituent
                       join ca in db.ConstituentAddress on c.ConstituentId equals ca.ConstituentId
                       join cp in db.ConstituentPhone on c.ConstituentId equals cp.ConstituentId
                       join ce in db.ConstituentEmail on c.ConstituentId equals ce.ConstituentId
                       where ca.ConstituentId == id
                          && c.ConstituentId == id
                          && cp.ConstituentId == id
                          && ce.ConstituentId == id
                       select new ConstituentDetailsView
                       {
                           zConstituent = c,
                           zConstituentAddress = ca,
                           zAddress = ca.Address,
                           zConstituentEmail = ce,
                           zEmail = ce.Email,
                           zConstituentPhone = cp,
                           zPhone = cp.Phone
                       };
            return View("ConstituentDetails/View_All", cape);
        }

查看:

@model IEnumerable<CodeFirstShenanigansWeb.Models.ConstituentDetailsView>

@{


 ViewBag.Title = "View All";
}

<h2>Details</h2>

   <h3>@Model.ElementAt(0).zConstituent.FirstName @Model.ElementAt(0).zConstituent.LastName</h3>
<h4>@Model.Count();</h4>

<h2><b>Addresses</b></h2>

@foreach (var item in Model)
{ 
<div>
    <div>@item.zAddress.Street</div>
    <div>@item.zAddress.City</div>
    <div>@item.zAddress.State</div>
    <div>@item.zAddress.Zip</div>
</div>

}
<h2><b>Phone Numbers</b></h2>   

@foreach (var item in Model)
{

    <div>
        <div>@item.zPhone.Number</div>
    </div>

}

<h2><b>Email Addresses</b></h2>

@foreach (var item in Model)
{

    <div>
        <div>@item.zEmail.Mail</div>
    </div>

<p>
    @Html.ActionLink("Edit", "EditAll", new { id = item.zConstituent.ConstituentId})|
    @Ajax.ActionLink("Delete", "DELETE", new { id = item.zConstituent.ConstituentId }, new AjaxOptions

                                                                                      {

                                                                                          Confirm = "Are you sure you want to delete?",

                                                                                          OnComplete = "deleteComplete",

                                                                                          HttpMethod = "DELETE"

                                                                                      })
    @Html.ActionLink("Back to List", "Index")|
</p>
}

视图模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CRM.DomainClasses.Enums;
using CRM.DomainClasses;

namespace CodeFirstShenanigansWeb.Models
{
    public class ConstituentDetailsView
    {
        public Constituent zConstituent { get; set; }
        public Address zAddress { get; set; }
        public Phone zPhone { get; set; }
        public Email zEmail { get; set; }
        public ConstituentPhone zConstituentPhone { get; set; }
        public ConstituentEmail zConstituentEmail { get; set; }
        public ConstituentAddress zConstituentAddress { get; set; }

    }
}

我想我需要更多的ViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CRM.DomainClasses;

namespace CodeFirstShenanigansWeb.Models
{
    public class ConstituentListDetailsView
    {
        public Constituent zConstituent { get; set; }
        public ICollection<Address> zAddress { get; set; }
        public ICollection<Phone> zPhone { get; set; }
        public ICollection<Email> zEmail { get; set; }
        public ConstituentPhone zConstituentPhone { get; set; }
        public ConstituentEmail zConstituentEmail { get; set; }
        public ConstituentAddress zConstituentAddress { get; set; }
    }

} 我感谢任何帮助。谢谢!

0 个答案:

没有答案