2013-05-02 91 views
0

我有一個LINQ查詢返回幾行。它有一個連接,因此它包含公司聯繫信息以及詳細記錄(因此每行重複的聯繫信息)。在我的視圖頁面上,我希望只在標題區域顯示聯繫人信息一次,然後循環訪問記錄集以顯示詳細數據。從第一記錄集訪問數據

如何在沒有循環的情況下訪問第一條記錄中的聯繫人數據?

有沒有更好的方法?我避免將所有聯繫人字段填充到ViewBag變量中,因爲我認爲用單個數據庫查詢來獲取它們會更加正確。

BTW獲取代碼塊在這裏工作的訣竅是什麼?我總是點擊代碼按鈕,然後直接從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> 
     } 

編輯 與Boossss思想工作,我想出了這個LINQ和視圖模型,但他們都不太讓我一個可行的解決方案。我得到一個單獨的記錄集,其中包含VendorStatusHistory記錄的子集合,這正是我想要的,它會拋出錯誤,試圖顯示視圖模型的任何屬性「不包含定義...並且不能找到擴展方法...」。我嘗試使用Boossss viewmodel,但它給出了同樣的錯誤。我懷疑我的LINQ查詢已關閉。

已解決 根據Boossss的評論更新。

 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> 
+0

發現一種解決方法是將聯繫信息包裝在foreach中幷包含一個break語句,以便它只循環一次。希望有人能提出一個更優雅的解決方案。 :) – SQLGrinder 2013-05-02 19:59:18

回答

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> 
    } 
+0

Thx,這是我正在尋找的方向,但不能得到正確的LINQ查詢和ViewModel使其工作(見上) – SQLGrinder 2013-05-03 20:54:53

+0

嗨維克,你的viewmodel看起來不錯:),但是你是什麼模型進入視圖?你傳遞一個IEnumerable 或一個StatusHistoryView?您應該傳遞一個StatusHistoryView。 – boossss 2013-05-03 21:18:56

+0

是的你是對的。我忽略了這一點,並從LINQ和查看頁面中刪除IEnumerable,並將SingleOrDefault添加到LINQ。這顯示了一切。但是,這給我顯示VendorStatusHistory行的標題標籤帶來了另外一個問題。爲了讓標題顯示出來,我不得不求助於單一的foreach(參見OP的更新) - 不夠高雅,但它讓我在99.9%的地方獲得了。 – SQLGrinder 2013-05-06 21:18:31