I'm trying to create a list view with data coming from different tables in my database . Following you have what I tried:
I created a LINQDataContex and referenced associations between my tables.
This has automatically created EntitySets inside my table's class in my designer.cs file like this:
[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);
}
}
Then I created a query in my controller to select the information and return it to my view:
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);
}
And finally I'm trying to display the data in my view, with no success, through this piece of code:
@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>
This returns me the following error:
'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?)
Please let me know what I'm doing wrong. Thanks a lot!
It looks like your View's Model is the wrong type. Your query isn't returning tblfLeaseDetail objects. It's returning a new, anonymous object you're creating in the select part of the query.
I would recommend creating a new class (perhaps called LeaseViewModel) with all of the properties you need. Then, change your query to return it instead of the anonymous type it's returning right now.
You can have your query return your view model by changing:
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 }
To something like:
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 }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.