我有一个返回几行的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和ViewModel,但他们并没有给我一个有效的解决方案。我得到一个包含VendorStatusHistory记录子集的记录集,这正是我想要的,它会在尝试显示viewmodel的任何属性时抛出错误“不包含定义......并且找不到扩展方法......”。我尝试使用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 :(得分:3)
我建议您没有加入公司的联系信息以及详细记录,而是创建一个viewmodel并将其作为您的视图模型传递:
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>
}