0

我有一个返回几行的 LINQ 查询。它有一个联接,因此它包括公司联系信息以及详细记录(因此每行都重复联系信息)。在我的视图页面上,我希望仅在标题区域中显示一次联系信息,然后循环遍历记录集以显示详细数据。

如何在不循环的情况下访问第一条记录中的联系人数据?

有更好的方法吗?我避免将所有联系人字段填充到 ViewBag 变量中,因为我认为使用单个数据库查询来获取它们会更正确。

顺便说一句,让代码块在这里工作的诀窍是什么。我总是单击代码按钮,然后直接从 VS 粘贴我的代码。有时颜色格式有效,有时无效。

    <fieldset>
    <legend>Contact Information</legend>
    <div class="view-field">
        @Html.ValueFor(m => m.CompanyName)
    </div>
    <div class="view-field">
        @Html.ValueFor(m => m.Name)
    </div>
    <div class="view-field">
        @Html.ValueFor(m => m.Address1)
    </div>
    <div class="view-field">
        @Html.ValueFor(m => m.City)
    </div>
    <div class="view-field">
        @Html.ValueFor(m => m.State)
    </div>
    <div class="view-field">
        @Html.ValueFor(m => m.Zip)
    </fieldset>


        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(m => item.StatusDate)
                </td>
                <td>
                    @Html.DisplayFor(m => item.StatusName)
                </td>
                <td>
                    @Html.DisplayFor(m => item.StatusUserName)
                </td>
                <td>
                    @Html.DisplayFor(m => item.StatusNote)
                </td>
            </tr>
        }

编辑 使用 Boosss 的想法,我想出了这个 LINQ 和 ViewModel,但它们并没有给我一个有效的解决方案。我得到一个带有 VendorStatusHistory 记录子集合的单个记录集,这正是我想要的,它在尝试显示视图模型的任何属性时抛出错误“不包含定义......并且没有扩展方法......可以找到。” 我尝试使用 Boosss 视图模型,但它给出了同样的错误。我怀疑我的 LINQ 查询已关闭。

已解决根据 Bossss 评论更新

     public StatusHistoryView VendorStatusHistory(String id)
    {
        Guid pid = Guid.Parse(id);

        StatusHistoryView query = _db.VendorProfiles 
            .Include("VendorStatusHistory")
            .Include("StatusCodes")
            .Select(s => new StatusHistoryView
                {
                    ProfileID = s.ProfileID,
                    Name = s.Name,
                    CompanyName = s.CompanyName,
                    CompanyDBA = s.CompanyDBA,
                    Email = s.Email,
                    Phone = s.Phone,
                    Website = s.Website,
                    Address1 = s.Address1,
                    Address2 = s.Address2,
                    City = s.City,
                    State = s.State,
                    Zip = s.Zip,
                    VendorStatusHistory = s.VendorStatusHistories
                }
            )
            .Where(x => x.ProfileID == pid).SingleOrDefault();;

         return query;
    }

public class StatusHistoryView
{
    public StatusHistoryView()
    {
        this.VendorStatusHistory = new HashSet<VendorStatusHistory>();
    }
    public System.Guid ProfileID { get; set; }
    public int StatusID { get; set; }

    [Display(Name = "Full Name")]
    public string Name { get; set; }

    [Display(Name = "Email")]
    public string Email { get; set; }

    [Display(Name = "Phone")]
    public string Phone { get; set; }

    [Display(Name = "Website")]
    public string Website { get; set; }

    [Display(Name = "Company")]
    public string CompanyName { get; set; }

    [Display(Name = "DBA")]
    public string CompanyDBA { get; set; }

    [Display(Name = "Address")]
    public string Address1 { get; set; }

    [Display(Name = "Address (extra)")]
    public string Address2 { get; set; }

    [Display(Name = "City")]
    public string City { get; set; }

    [Display(Name = "State")]
    public string State { get; set; }

    [Display(Name = "Zip Code")]
    public string Zip { get; set; }

    public virtual ICollection<VendorStatusHistory> VendorStatusHistory { get; set; }
}

和更新的视图...

            @model  VendorProfileIntranet.Models.StatusHistoryView

<table id="VendorTable" width="100%" class="gradeA">
    <thead>
        @foreach (var item in Model.VendorStatusHistory)
        {
        <tr>
            <th style="width:200px">
                @Html.DisplayNameFor(m => item.DateCreated)
            </th>
            <th>
                @Html.DisplayNameFor(m => item.Status)
            </th>
            <th>
                @Html.DisplayNameFor(m => item.UserName)
            </th>
            <th>
                @Html.DisplayNameFor(m => item.Notes)
            </th>
        </tr>
            break;
            }
    </thead>
    <tbody>

@foreach (var item in Model.VendorStatusHistory )
        {
            <tr>
                <td>
                    @Html.DisplayFor(m => item.StatusDate)
                </td>
                <td>
                    @Html.DisplayFor(m => item.StatusName)
                </td>
                <td>
                    @Html.DisplayFor(m => item.StatusUserName)
                </td>
                <td>
                    @Html.DisplayFor(m => item.StatusNote)
                </td>
            </tr>

        }
    </tbody>
    </tbody>
</table>
4

1 回答 1

3

我建议您不要将公司联系信息与详细记录一起加入,而是创建一个视图模型并将其作为您的视图模型传递,如下所示:

public class MyViewModel
{
    public string CompanyName { get; set; }
    public string Name { get; set; }
    public string Address1 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }

    public List<RecordDetail> Records { get; set; }
}

public class RecordDetail
{
    public DateTime StatusDate { get; set; }
    public string StatusName { get; set; }
    public string StatusUserName { get; set; }
    public string StatusNote { get; set; }
}

在此之后,您可以像这样在视图中使用它:

@model MyViewModel
<fieldset>
<legend>Contact Information</legend>
<div class="view-field">
    @Html.ValueFor(m => m.CompanyName)
</div>
<div class="view-field">
    @Html.ValueFor(m => m.Name)
</div>
<div class="view-field">
    @Html.ValueFor(m => m.Address1)
</div>
<div class="view-field">
    @Html.ValueFor(m => m.City)
</div>
<div class="view-field">
    @Html.ValueFor(m => m.State)
</div>
<div class="view-field">
    @Html.ValueFor(m => m.Zip)
</fieldset>


    @foreach (var item in Model.Records)
    {
        <tr>
            <td>
                @Html.DisplayFor(m => item.StatusDate)
            </td>
            <td>
                @Html.DisplayFor(m => item.StatusName)
            </td>
            <td>
                @Html.DisplayFor(m => item.StatusUserName)
            </td>
            <td>
                @Html.DisplayFor(m => item.StatusNote)
            </td>
        </tr>
    }
于 2013-05-02T22:59:19.513 回答