代码之家  ›  专栏  ›  技术社区  ›  kevorski

在单视图mvc中显示多个表

  •  0
  • kevorski  · 技术社区  · 8 年前

    我有两个相互有关系的表。我希望能够在单个视图中同时查看信息。但是,当我尝试使用VolumeRates表中的属性时,会出现以下错误:

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

    我的模型的代码是

    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> index.cshtml ?

    1 回复  |  直到 8 年前
        1
  •  1
  •   SSharp    8 年前

    为了解决这个问题,您尝试将集合推入一个十进制的BidRate字段。您需要使用FirstOrDefault()或任何需要过滤的单一结果,将VolumeRates折叠为单个对象,然后获取BidRate:

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