MVC变量未显示在视图中
本文关键字:视图 显示 变量 MVC | 更新日期: 2023-09-27 17:59:21
我有4个相关的类,我试图显示这些类的信息,但底层类属性的值没有显示在视图中,尽管在调试时该值可用,但它从未传输到页面。我的代码如下:
控制器
// 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>
}
型号顶级
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; }
}
}
型号级别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; }
}
}
型号级别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; }
}
}
型号级别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; }
}
}
由于没有其他人会从评论中回答,我会的。
尝试更改:
<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=>为对象中由字符串表达式表示的每个属性返回Html标记。
Html.DisplayFor=>为对象中由System.Linq.Expression表达式表示的每个属性返回Html标记。
更新:
如果你想使用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>
您将不得不使用SelectMany
,因为Meter
的类型已经是ICollection
。。所以如果你只使用Select
,你会沿着List<List<Meter>[]>
生成一些东西。。所以SelectMany
会把它变平,变成一个List<Meter>
,这就是你想要的。