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

时间:2015-10-14 20:55:32

标签: c# .net linq join razor

我正在尝试使用来自数据库中不同表格的数据创建列表视图。跟着你的尝试:

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

Dbml File

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

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

1 个答案:

答案 0 :(得分:1)

看起来你的View模型是错误的类型。您的查询未返回tblfLeaseDetail对象。它将返回您在查询的选择部分中创建的新的匿名对象。

我建议使用您需要的所有属性创建一个新类(可能称为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 }