在视图DisplayNameFor中显示LINQ连接表列

本文关键字:连接 LINQ 显示 视图 DisplayNameFor | 更新日期: 2023-09-27 18:17:40

我试图创建一个列表视图,数据来自不同的表在我的数据库。下面是我试过的:

我创建了一个LINQDataContex并引用了我的表之间的关联。

Dbml文件

在我的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?)

请让我知道我做错了什么。非常感谢!

在视图DisplayNameFor中显示LINQ连接表列

看起来你的视图模型是错误的类型。您的查询没有返回tblfLeaseDetail对象。它返回一个你在查询的select部分创建的新的匿名对象。

我建议创建一个包含所有所需属性的新类(可能称为LeaseViewModel)。然后,修改你的查询返回它,而不是现在返回的匿名类型。

你可以让你的查询返回你的视图模型通过改变:

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 }

转到如下:

select new LeaseViewModel() { Detail_ID = l.Lease_Detail_ID, Lease_ID = l.Lease_ID, XRef_Lease_ID = l.XRef_Lease_ID, Vendor_Name=v.Vendor_Name, Description = l.Description, County = c.County, Amount = l.Amount, Payment_Due_Date = l.Payment_Due_Date, Authorized = a.Authorized, Line_Type = t.Line_Type }