我在razor视图中有这个奇怪的空引用异常,这是我的视图模型代码
public class RoomWantedAd
{
private List<ItemResource> _ItemResources = null;
public RoomWantedAd()
{
_ItemResources = new List<ItemResource>();
}
public int RoomWantedId { get; set; }
public Item Item { get; set; }
public ItemDescription ItemDescription { get; set; }
public ItemWanted ItemWanted { get; set; }
public ItemLocation ItemLocation { get; set; }
public ItemAddress ItemAddress { get; set; }
public string CityName { get; set; }
public string AreaName { get; set; }
public List<ItemResource> ItemResources
{
get { return _ItemResources; }
set { _ItemResources = value; }
}
public string PlaceId { get; set; }
public string strCanMoveInFrom { get; set; }
}
和我的数据库模型ItemWanted.cs代码在这里
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("ItemWanted")]
public partial class ItemWanted
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public ItemWanted()
{
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ItemWantedId { get; set; }
[ForeignKey("Item")]
public int ItemId { get; set; }
public int? Budget { get; set; }
[StringLength(10)]
public string BudgetTimeSpan { get; set; }
public DateTime? CanMoveInFrom { get; set; }
[StringLength(3)]
public string DaysToStay { get; set; }
public bool? ParkingAmenity { get; set; }
public bool? BalconyAmenity { get; set; }
public bool? GardenAmenity { get; set; }
public bool? IsAmenityEnabled { get; set; }
public bool? GarageAmenity { get; set; }
public bool? BroadBandAmenity { get; set; }
public bool? FurnishedAmenity { get; set; }
[StringLength(15)]
public string YourOccupation { get; set; }
[StringLength(3)]
public string Age1 { get; set; }
[StringLength(3)]
public string Age2 { get; set; }
public bool? Smoker { get; set; }
public bool? Pets { get; set; }
[StringLength(20)]
public string Language { get; set; }
[StringLength(20)]
public string Nationality { get; set; }
[StringLength(20)]
public string Interests { get; set; }
[StringLength(20)]
public string DisplayName { get; set; }
[StringLength(15)]
public string FlatMateGender { get; set; }
public bool? FlatMateSmoking { get; set; }
[StringLength(3)]
public string FlatMateAge1 { get; set; }
[StringLength(3)]
public string FlatMateAge2 { get; set; }
public bool? FlatMatePets { get; set; }
[StringLength(15)]
public string FlatMateOccupation { get; set; }
public int? NumberOfViews { get; set; }
[StringLength(15)]
public string YourGender { get; set; }
public virtual Item Item { get; set; }
}
我的剃刀视图代码在这里
</div>
<div class="row">
<div class="col-md-9 text-left">
<span>£</span> @Html.Label(RoomFlogContents.Classes.SearchUtility.GetPriceLabelItemWanted(item))
<br />
@Html.Label(item.ItemWanted.Item.ItemAddress.FormattedAddress, new { @class = "h4 inline-block" })
@{
DateTime dt = DateTime.Now;
if (item.ItemWanted.CanMoveInFrom != null)
{
dt = (DateTime)item.ItemWanted.CanMoveInFrom;
}
}
<br />
<label>Can move in from:</label>
@Html.Label(dt.ToString("dd/MM/yyyy"), new { @class = "h4 inline-block" })
</div>
</div>
所有代码运行正常,但是当我的剃刀视图代码执行时,在行
@Html.Label(item.ItemWanted.Item.ItemAddress.FormattedAddress, new { @class = "h4 inline-block" })
和
@Html.Label(dt.ToString("dd/MM/yyyy"), new { @class = "h4 inline-block" })
我的代码崩溃并提供了一个空引用异常,我通过断点检查了属性是否为null,但每个实例属性都是正确实例化并包含值。
我也使用了null检查,但它仍然抛出异常。请有人帮助我。