简体   繁体   English

MVC变量未在视图中显示

[英]MVC variables not displaying in view

I have 4 related classes which I am attempting to display information on but the value of the bottom level class attributes are not being displayed in the view despite the value being available when debugging it is never being transferred to the page my code is as follows: 我有4个相关的类,我试图在这些类上显示信息,但是底层类属性的值未在视图中显示,尽管该值在调试时始终可用,但从未传输到页面上,我的代码如下:

CONTROLLER CONTROLLER

// GET: MarketMessages
public ActionResult Index()
{
    var test = db.MarketMessage.Where(m => m.messageType == "300S")
    .Include(m => m.messageType300S
    .Select(mt => mt.Meter
    .Select(r => r.RegisterLevelInformation )))
    .ToList();

    return View(test);
}

VIEW 视图

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.CreatedOn)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.messageType)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.VersionNumber)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MarketTimestamp)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TxRefNbr)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Sender)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Recipient)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.alertFlag)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.fileName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.IsDeleted)
        </td>
        <td>
            @foreach (var m in item.messageType300S.Select(m => m.Meter).FirstOrDefault())
            {
                @Html.Display(m.SerialNumber)
            }
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.MarketMessageID }) |
            @Html.ActionLink("Details", "Details", new { id=item.MarketMessageID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.MarketMessageID })
        </td>
    </tr>
}

MODEL TOP LEVEL 模型最高水平

namespace BillingEngine.DAL
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("MarketMessage")]
    public partial class MarketMessage
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public MarketMessage()
        {
            messageType300 = new HashSet<messageType300>();
            messageType300S = new HashSet<messageType300S>();
            messageType300W = new HashSet<messageType300W>();
            messageType305 = new HashSet<messageType305>();
        }

        public int MarketMessageID { get; set; }

        public DateTime CreatedOn { get; set; }

        [Required]
        [StringLength(4)]
        public string messageType { get; set; }

        [Required]
        [StringLength(8)]
        public string VersionNumber { get; set; }

        public DateTime MarketTimestamp { get; set; }

        [Required]
        [StringLength(35)]
        public string TxRefNbr { get; set; }

        [Required]
        [StringLength(3)]
        public string Sender { get; set; }

        [Required]
        [StringLength(3)]
        public string Recipient { get; set; }

        [StringLength(10)]
        public string alertFlag { get; set; }

        [StringLength(50)]
        public string fileName { get; set; }

        public bool IsDeleted { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<messageType300> messageType300 { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<messageType300S> messageType300S { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<messageType300W> messageType300W { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<messageType305> messageType305 { get; set; }
    }
}

MODEL LEVEL 2 模型级别2

namespace BillingEngine.DAL
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    public partial class messageType300S
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public messageType300S()
        {
            Meter = new HashSet<Meter>();
            UsageFactor = new HashSet<UsageFactor>();
        }

        public int messageType300SID { get; set; }

        public int MarketMessageID { get; set; }

        [Required]
        [StringLength(11)]
        public string MPRN { get; set; }

        [StringLength(35)]
        public string MPBR { get; set; }

        [Required]
        [StringLength(35)]
        public string NetworkReferenceNumber { get; set; }

        [Required]
        [StringLength(3)]
        public string LoadProfileCode { get; set; }

        [Required]
        [StringLength(4)]
        public string DUoSGroup { get; set; }

        [Required]
        [StringLength(2)]
        public string MeterPointStatusCode { get; set; }

        public DateTime ReadDate { get; set; }

        public virtual MarketMessage MarketMessage { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Meter> Meter { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<UsageFactor> UsageFactor { get; set; }
    }
}

MODEL LEVEL 3 模型级别3

namespace BillingEngine.DAL
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("Meter")]
    public partial class Meter
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Meter()
        {
            RegisterLevelInformation = new HashSet<RegisterLevelInformation>();
        }

        public int MeterID { get; set; }

        [StringLength(15)]
        public string MeterCategoryCode { get; set; }

        [Required]
        [StringLength(9)]
        public string SerialNumber { get; set; }

        [StringLength(3)]
        public string MeterLocationCode { get; set; }

        public int? MessageType300ID { get; set; }

        public int? MessageType300SID { get; set; }

        public int? MessageType300WID { get; set; }

        public int? MessageType303RID { get; set; }

        public int? MessageType305ID { get; set; }

        public int? MessageType306ID { get; set; }

        public int? MessageType306WID { get; set; }

        public int? MessageType307ID { get; set; }

        public int? MessageType307WID { get; set; }

        public int? MessageType310ID { get; set; }

        public int? MessageType310WID { get; set; }

        public int? MessageType320ID { get; set; }

        public int? MessageType320WID { get; set; }

        public int? MessageType341ID { get; set; }

        public int? MessageType342ID { get; set; }

        public virtual messageType300 messageType300 { get; set; }

        public virtual messageType300S messageType300S { get; set; }

        public virtual messageType300W messageType300W { get; set; }

        public virtual messageType305 messageType305 { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<RegisterLevelInformation> RegisterLevelInformation { get; set; }
    }
}

MODEL LEVEL 4 模型级别4

namespace BillingEngine.DAL
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("RegisterLevelInformation")]
    public partial class RegisterLevelInformation
    {
        public int RegisterLevelInformationID { get; set; }

        [Required]
        [StringLength(3)]
        public string MeterRegisterSequence { get; set; }

        [Required]
        [StringLength(10)]
        public string TimeslotCode { get; set; }

        [Required]
        [StringLength(3)]
        public string UOM_Code { get; set; }

        public decimal MeterMultiplier { get; set; }

        public decimal ReadingValue { get; set; }

        [Required]
        [StringLength(2)]
        public string ReadReasonCode { get; set; }

        [Required]
        [StringLength(2)]
        public string ReadTypeCode { get; set; }

        public DateTime? PreviousReadDate { get; set; }

        public decimal Consumption { get; set; }

        public decimal EstimatedConsumption { get; set; }

        [StringLength(4)]
        public string ReadStatusCode { get; set; }

        [StringLength(2)]
        public string RegisterTypeCode { get; set; }

        [StringLength(1)]
        public string PostDecimalDetails { get; set; }

        [StringLength(2)]
        public string PreDecimalDetails { get; set; }

        public int? MeterID { get; set; }

        public int? RemovedMeterRegisterID { get; set; }

        public int? NewMeterRegisterID { get; set; }

        public int? RetainedMeterRegisterID { get; set; }

        public virtual Meter Meter { get; set; }
    }
}

Since, no one else is going to answer from the comments, I will. 因为,没有其他人会从评论中回答 ,所以我会的。

Try changing this: 尝试更改此:

<td>
    @foreach (var m in item.messageType300S.Select(m => m.Meter).FirstOrDefault())
    {
        @Html.Display(m.SerialNumber)
    }
</td>

To: 至:

<td>
    @foreach (var m in item.messageType300S.Select(m => m.Meter).FirstOrDefault())
    {
        @Html.DisplayFor(x => m.SerialNumber)
    }
</td>

Html.Display => Returns HTML markup for each property in the object that is represented by a string expression. Html.Display =>返回对象中由字符串表达式表示的每个属性的HTML标记。

Html.DisplayFor => Returns HTML markup for each property in the object that is represented by the System.Linq.Expressions.Expression expression. Html.DisplayFor =>返回由System.Linq.Expressions.Expression表达式表示的对象中每个属性的HTML标记。

UPDATE: 更新:

If you are looking into looping through each Meter using ToList() and then displaying details of each meter then can do this: 如果要使用ToList()遍历每个Meter ,然后显示每个仪表的详细信息,则可以这样做:

<td>
    @foreach (var m in item.messageType300S.SelectMany(y => y.Meter).ToList())
    {
        @Html.DisplayFor(y => m.SerialNumber)
        @Html.DisplayFor(y => m.OtherPropertyDetail) 
        @Html.DisplayFor(y => m.AnotherPropertyDetail)
    }
</td>

You are going to have to use SelectMany since Meter is already of type ICollection .. so if you just use Select you will generate something along these lines List<List<Meter>[]> .. so SelectMany will flatten that out to just a List<Meter> which is what you are looking for. 您将必须使用SelectMany因为Meter已经是ICollection类型的..因此,如果您仅使用Select您将沿着这些行生成一些东西List<List<Meter>[]> ..因此SelectMany会将其展平为一个List<Meter>这是您要寻找的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM