【发布时间】:2015-10-14 20:55:32
【问题描述】:
我正在尝试使用来自我数据库中不同表的数据创建一个列表视图。跟随你有我尝试过的:
我创建了一个 LINQDataContex 并引用了我的表之间的关联。
这会自动在我的 Designer.cs 文件中的表类中创建 EntitySets,如下所示:
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="tblfLeaseDetail_tblvVendor", Storage="_tblvVendors", ThisKey="Vendor_ID", OtherKey="Vendor_ID")]
public EntitySet<tblvVendor> tblvVendors
{
get
{
return this._tblvVendors;
}
set
{
this._tblvVendors.Assign(value);
}
}
然后我在控制器中创建了一个查询来选择信息并将其返回到我的视图中:
public ActionResult Leases()
{
LeasesLINQDataContext leases = new LeasesLINQDataContext();
var leaseList = (from l in leases.tblfLeaseDetails
join a in leases.tblfAuthorizations on l.Lease_Detail_ID equals a.Lease_Detail_ID
join c in leases.tblvCounties on l.County_ID equals c.County_ID
join t in leases.tblvLineTypes on l.Line_Type_ID equals t.Line_Type_ID
join v in leases.tblvVendors on l.Vendor_ID equals v.Vendor_ID
select new {l.Lease_Detail_ID, l.Lease_ID, l.XRef_Lease_ID, v.Vendor_Name, l.Description, c.County,
l.Amount, l.Payment_Due_Date, a.Authorized, t.Line_Type }).ToList();
ViewBag.Message = "Your app description page.";
return View(leaseList);
}
最后我试图通过这段代码在我的视图中显示数据,但没有成功:
@model IEnumerable<LMWEB_MVC.tblfLeaseDetail>
@{
ViewBag.Title = "Leases";
}
<h2>Leases</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Lease_ID)
</th>
<th>
@Html.DisplayNameFor(model => model.XRef_Lease_ID)
</th>
<th>
@Html.DisplayNameFor(model => model.Lease_Type)
</th>
<th>
@Html.DisplayNameFor(model => model.Company_ID)
</th>
<th>
@Html.DisplayNameFor(model => model.Vendor_Name)
</th>
这会返回以下错误:
'System.Collections.Generic.IEnumerable<LMWEB_MVC.tblfLeaseDetail>' does not contain a definition for 'Vendor_Name' and no extension method 'Vendor_Name' accepting a first argument of type 'System.Collections.Generic.IEnumerable<LMWEB_MVC.tblfLeaseDetail>' could be found (are you missing a using directive or an assembly reference?)
请让我知道我做错了什么。非常感谢!
【问题讨论】: