MVC变量不在视图中显示

时间:2016-07-29 11:44:17

标签: c# asp.net-mvc entity-framework parent-child

我有4个相关的类,我试图显示信息,但是底层类属性的值没有显示在视图中,尽管在调试时它的值是可用的,它永远不会被传输到页面我的代码是如下:

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);
}

查看

@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

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

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

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; }
    }
}

1 个答案:

答案 0 :(得分:2)

因为,没有其他人会从评论中回答,我会。

尝试更改此内容:

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

要:

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

Html.Display =&gt;返回由字符串表达式表示的对象中每个属性的HTML标记。

     

Html.DisplayFor =&gt;返回由System.Linq.Expressions.Expression表达式表示的对象中每个属性的HTML标记。

<强>更新

如果您正在考虑使用Meter循环遍历每个ToList(),然后显示每个计量表的详细信息,那么可以执行以下操作:

<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>

您将不得不使用SelectMany,因为Meter已经是ICollection类型..所以,如果您只使用Select,您将在这些行中生成内容{ {1}} ..所以List<List<Meter>[]>会将其简化为SelectMany,这正是您所寻找的。