在单个视图mvc中显示多个表

时间:2018-04-09 22:26:49

标签: c# asp.net-mvc viewmodel

我有两张彼此有关系的牌桌。我希望能够在一个视图中同时查看信息。但是,当我尝试使用VolumeRates表中的属性时,我收到错误:

  

错误CS1061'ICollection'不包含'BidRate'的定义,并且没有可以找到接受类型'ICollection'的第一个参数的扩展方法'BidRate'(您是否缺少using指令或程序集引用?)TimberSales C:\ Users \ kraskell.CDATRIBE2 \ Documents \ Visual Studio 2017 \ Projects \ TimberSales \ TimberSales \ Controllers \ DataEntryController.cs 18 Active

我的模型的代码是

.put_@()

这是using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using TimberSales.Models; namespace TimberSales.Controllers { public class DataEntryController : Controller { // GET: DataEntry public ActionResult Index() { TimberSalesEntities db = new TimberSalesEntities(); List<TimberSale> timberSales = db.TimberSales.ToList(); List<DataEntry> dataEntries = timberSales.Select(x => new DataEntry { ContractNumberTimberSales = x.ContractNumber, BidRate = x.VolumesRates.BidRate }).ToList(); return View(dataEntries); } } } 文件

Index.cshtml

这是@model IEnumerable<TimberSales.Models.DataEntry> @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Index</h2> <p> @Html.ActionLink("Create New", "Create") </p> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.ContractNumberTimberSales) </th> <th> @Html.DisplayNameFor(model => model.BidRate) </th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.ContractNumberTimberSales) </td> <td> @Html.DisplayFor(modelItem => item.BidRate) </td> <td> @Html.ActionLink("Edit", "Edit", new { id=item.ContractNumberTimberSales }) | @Html.ActionLink("Details", "Details", new { id=item.ContractNumberTimberSales }) | @Html.ActionLink("Delete", "Delete", new { id=item.ContractNumberTimberSales }) </td> </tr> } </table> 类文件:

DataEntry

using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; namespace TimberSales.Models { public class DataEntry { public string ContractNumberTimberSales { get; set; } public string LoggingUnit { get; set; } public string TractNumber { get; set; } public string Seller { get; set; } public string ApprovingOfficer { get; set; } public int NumBids { get; set; } public decimal TotalTractValue { get; set; } public decimal TotalTractVolume { get; set; } public Nullable<decimal> AcreageHarvested { get; set; } public string SilviculturalTreatment { get; set; } public string HarvestReason { get; set; } public Nullable<System.DateTime> BidDate { get; set; } public Nullable<System.DateTime> CutPayDate { get; set; } public Nullable<System.DateTime> ContractApprovedDate { get; set; } public Nullable<System.DateTime> ExtensionApprovedDate { get; set; } public Nullable<System.DateTime> ExtensionCuttingEndsDate { get; set; } public Nullable<System.DateTime> ExtentionExpirationDate { get; set; } public decimal EstimatedBidValue { get; set; } public decimal TotalAmountReceived { get; set; } public decimal AdminExpenseDeduction { get; set; } public decimal AdminExpenseDeductionPercent { get; set; } public Nullable<System.DateTime> SaleClosureDate { get; set; } public string Remarks { get; set; } public decimal ContractAmount { get; set; } public string ContractorName { get; set; } public virtual Contractor Contractor { get; set; } public int Species { get; set; } public int SawProduct { get; set; } public Nullable<decimal> EstimatedVolume { get; set; } public Nullable<decimal> ActualVolume { get; set; } public Nullable<decimal> BaseRate { get; set; } public Nullable<decimal> AppraisalRate { get; set; } public Nullable<decimal> AdvertisedRate { get; set; } public decimal BidRate { get; set; } public string ContractNumberVolumeRates { get; set; } public virtual SawProduct SawProduct1 { get; set; } public virtual Species Species1 { get; set; } public virtual TimberSale TimberSale { get; set; } } } 对象包含TimberSales个对象的列表

我是否需要执行VolumeRates才能访问我需要的信息,或者我可以在select join语句中执行此操作以显示我需要的信息,或者我是否需要修改TimberSales.Select

中的@model IEnumerable<TimberSales.Models.DataEntry>

1 个答案:

答案 0 :(得分:1)

要解决此问题,您尝试将集合推送到BidRate字段,该字段是小数。您需要将VolumeRates折叠为单个对象,然后使用FirstOrDefault()或您需要过滤的任何单个结果来获取BidRate:

List<DataEntry> dataEntries = timberSales.Select(x => new DataEntry { ContractNumberTimberSales = x.ContractNumber, BidRate = x.VolumesRates.FirstOrDefault().BidRate }).ToList();