2015-10-14 84 views
1

我試圖創建一個列表視圖,其數據來自我的數據庫中的不同表格。以下你有我嘗試過的:顯示LINQ列表查看列表顯示名稱

我創建了一個LINQDataContex並引用了我的表之間的關聯。

Dbml File

這已經自動創建我的表的類中EntitySets在我的designer.cs文件中像這樣:

[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

看起來您的視圖模型是錯誤的類型。您的查詢不返回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 } 
+0

傑森,非常感謝回答!我做了你的建議,但現在leaseList無法接收查詢,因爲VS無法將Anonymous類型轉換爲LeaseViewModel。當你說「改變你的查詢返回它」你的意思是改變我的變量的類型爲LeaseViewModel,對吧? –

+0

看起來我在這個鏈接上找到了解決方案:http://www.codeproject.com/Articles/38635/Converting-anonymous-types-to-any-type –

+0

我添加了一個選擇LeaseViewModel對象而不是匿名類型。 –