Linq的ViewModel查询不是DomainModel

时间:2019-04-05 17:05:00

标签: linq asp.net-mvc-5 entity-framework-6

在Google上查找使用MVC Crud设置的示例时,我总是发现细节页面被认为是重要的,特别是如果您要使用ViewModel的话。

我遇到的问题是,Visual Studio中的典型MVC应用程序设置将使用CRUD,但是所有视图页面都将使用域模型而不是视图模型。

所以我想基于整数easy从两个模型中提取数据,如果它是一个模型:


        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Asset Assets = db.Assets.Find(id);

            if (id == null)
            {
                return HttpNotFound();
            }
            return View();
        }

但是我该如何拉两套呢?这是我在viewmodel上的linq查询:


var ASSPATVM = (from s in db.Assets
                        join cp in db.PATs on s.AssetID equals cp.AssetID into AP
                        from subpat in AP.DefaultIfEmpty()


                        select new ASSPATVM
                        {
                            AssetID = s.AssetID,
                            ProductName = s.ProductName,
                            ModelName = s.ModelName,
                            SupplierName = s.SupplierName,
                            ManufacturerName = s.ManufacturerName,
                            SerialNo = s.SerialNo,
                            PurchaseDate = s.PurchaseDate,
                            PoNo = s.PoNo,
                            Costing = s.Costing,
                            TeamName = s.TeamName,
                            StaffName = s.StaffName,
                            InspectionDocumnets = subpat.InspectionDocumnets ?? String.Empty,
                            InspectionOutcomeResult = subpat.InspectionOutcomeResult
                        });

编辑

所以我尝试了:

 public ActionResult Details(int? AssetID)
        {
            var ASSPATINCVM = (from s in db.Assets
                                  join cp in db.PATs on s.AssetID equals cp.AssetID into AP
                                  from subASSPAT in AP.DefaultIfEmpty()
                                        join ci in db.INSs on s.AssetID equals ci.AssetID into AI
                                        from subASSINC in AI.DefaultIfEmpty()


                                        select new ASSPATINCVM()
                                  {
                                      AssetID = s.AssetID,
                                      ProductName = s.ProductName,
                                      ModelName = s.ModelName,
                                      SupplierName = s.SupplierName,
                                      ManufacturerName = s.ManufacturerName,
                                      SerialNo = s.SerialNo,
                                      PurchaseDate = s.PurchaseDate,
                                      PoNo = s.PoNo,
                                      Costing = s.Costing,
                                      TeamName = s.TeamName,
                                      StaffName = s.StaffName,
                                      WarrantyEndDate = subASSPAT.WarrantyEndDate,
                                      InspectionDate = subASSPAT.InspectionDate,
                                      InspectionOutcomeResult = subASSPAT.InspectionOutcomeResult,
                                      InspectionDocumnets = subASSPAT.InspectionDocumnets ?? String.Empty,
                                      InspectionDueDate = subASSPAT.InspectionDueDate
                                        });

            return View(ASSPATINCVM);
        }

我得到了错误:

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[Assets.Areas.CCS.Models.ASSPATINCVM]', but this dictionary requires a model item of type 'Assets.Areas.CCS.Models.ASSPATINCVM'.

所以我尝试列出()

InspectionDate = subASSPAT.InspectionDate,
                                      InspectionOutcomeResult = subASSPAT.InspectionOutcomeResult,
                                      InspectionDocumnets = subASSPAT.InspectionDocumnets ?? String.Empty,
                                      InspectionDueDate = subASSPAT.InspectionDueDate
                                        }).List();

@model List<Assets.Areas.CCS.Models.ASSPATINCVM>

但是现在投诉是

List<ASSPATINCVM>' does not contain a definition for 'ModelName'

更新

ViewModel:

using Assets.Models;
using System;
using System.ComponentModel.DataAnnotations;

namespace Assets.Areas.CCS.Models
{
    public class ASSPATINCVM
    {


        public int AssetID { get; set; }
        public string SerialNo { get; set; }
        public string PoNo { get; set; }
        public float? Costing { get; set; }
        public string InspectionDocumnets { get; set; }

        public Product ProductName { get; set; }
        public InspectionOutcome InspectionOutcomeResult { get; set; }
        public Model ModelName { get; set; }
        public BudgetCode Code { get; set; }
        public AssetType AssetTypeName { get; set; }
        public Manufacturer ManufacturerName { get; set; }
        public Staff StaffName { get; set; }
        public Team TeamName { get; set; }
        public Supplier SupplierName { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yy}", ApplyFormatInEditMode = true)]
        public DateTime? PurchaseDate { get; set; }
        public DateTime? WarrantyEndDate { get; set; }
        public DateTime? InspectionDate { get; set; }
        public DateTime? InspectionDueDate { get; internal set; }
    }
}

“详细信息”页面的一部分:

@model Assets.Areas.CCS.Models.ASSPATINCVM

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<div>
    <h4>ClinicalAsset</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.ModelName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.ModelName)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.AssetTypeName)
        </dt>

        <dd>

2 个答案:

答案 0 :(得分:1)

您的控制器操作返回一个IEnumerable<ASSPATINCVM>,但是您的视图需要一个ClinicalASSPATINCVM的实例,请在视图中查看以下行:

@model Assets.Areas.Clinical.Models.ClinicalASSPATINCVM

对于模型类IEnumerable,您应该能够将视图更新为相应的ASSPATINCVM类型,然后遍历列表,类似于:

@model IEnumerable<Assets.Areas.CCS.Models.ASSPATINCVM>

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<div>
    <h4>ClinicalAsset</h4>
    <hr />
    <dl class="dl-horizontal">
    @foreach(var m in Model)
    {
        <dt>
            @Html.DisplayNameFor(model => model[0].ModelName)
        </dt>
        <dd>
            @m.ModelName
        </dd>
    }
    </dl>
</div>

编辑:

基于注释,您需要更新查询以返回ASSPATINCVM的单个实例,可以通过以下方式进行操作:

    public ActionResult Details(int? AssetID)
    {
        var ASSPATINCVM = (from s in db.Assets
                              join cp in db.PATs on s.AssetID equals cp.AssetID into AP
                              from subASSPAT in AP.DefaultIfEmpty()
                                    join ci in db.INSs on s.AssetID equals ci.AssetID into AI
                                    from subASSINC in AI.DefaultIfEmpty()


                                    select new ASSPATINCVM()
                              {
                                  AssetID = s.AssetID,
                                  ProductName = s.ProductName,
                                  ModelName = s.ModelName,
                                  SupplierName = s.SupplierName,
                                  ManufacturerName = s.ManufacturerName,
                                  SerialNo = s.SerialNo,
                                  PurchaseDate = s.PurchaseDate,
                                  PoNo = s.PoNo,
                                  Costing = s.Costing,
                                  TeamName = s.TeamName,
                                  StaffName = s.StaffName,
                                  WarrantyEndDate = subASSPAT.WarrantyEndDate,
                                  InspectionDate = subASSPAT.InspectionDate,
                                  InspectionOutcomeResult = subASSPAT.InspectionOutcomeResult,
                                  InspectionDocumnets = subASSPAT.InspectionDocumnets ?? String.Empty,
                                  InspectionDueDate = subASSPAT.InspectionDueDate
                                    }).FirstOrDefault();

        return View(ASSPATINCVM);
    }

答案 1 :(得分:0)

基于jcruz答案:

我还需要这样在id和AssetID之间添加链接:

var ASSPATINCVM = (from s in db.Assets
                                       join cp in db.PATs on s.AssetID equals cp.AssetID into AP
                                       from subASSPAT in AP.DefaultIfEmpty()
                                       join ci in db.INSs on s.AssetID equals ci.AssetID into AI
                                       from subASSINC in AI.DefaultIfEmpty()


                                       select new ASSPATINCVM
                                       {
                                           AssetID = s.AssetID,
                                           AssetTypeName = s.AssetTypeName,
                                           ProductName = s.ProductName,
                                           ModelName = s.ModelName,
                                           SupplierName = s.SupplierName,
                                           ManufacturerName = s.ManufacturerName,
                                           SerialNo = s.SerialNo,
                                           PurchaseDate = s.PurchaseDate,
                                           PoNo = s.PoNo,
                                           Costing = s.Costing,
                                           TeamName = s.TeamName,
                                           StaffName = s.StaffName,
                                           WarrantyEndDate = subASSPAT.WarrantyEndDate,
                                           InspectionDate = subASSPAT.InspectionDate,
                                           InspectionOutcomeResult = subASSPAT.InspectionOutcomeResult,
                                           InspectionDocumnets = subASSPAT.InspectionDocumnets ?? String.Empty,
                                           InspectionDueDate = subASSPAT.InspectionDueDate
                                       }).FirstOrDefault(x => x.AssetID == id);