ViewModel错误:未将对象引用设置为对象的实例

时间:2014-09-15 15:33:22

标签: c# asp.net asp.net-mvc nullreferenceexception

我无法理解为什么会收到此错误,请您帮我找一下我做错了什么?

我想用这个ViewModel实现的目的是因为我有许多记录使用相同的CartId将其用作父项并将booking.Item.ItemName用作子项。

请告诉我,我试图做的方式是否正确?

控制器:

public ActionResult MyBookings()
{

    var myBookingList = SystemDB.Carts.Where(s => s.Booking.Login == strLoginName).OrderByDescending(s => s.RecordId).Take(10);


    List<MyBookingsViewModel> result = new List<MyBookingsViewModel>();
    foreach (var item in myBookingList)
    {
        MyBookingsViewModel model = new MyBookingsViewModel();

        model.Carts.RecordId = item.RecordId;
        model.Carts.CartId = item.CartId;
        model.Carts.Booking.Item.ItemName = item.Booking.Item.ItemName;
        model.Carts.Booking.UserFullName = item.Booking.UserFullName;
        model.Carts.Booking.RequestDate = item.Booking.RequestDate;
        model.Carts.StatusCode.StatusCodeName = item.StatusCode.StatusCodeName;
        result.Add(model);
    }


    return View(result);
}

型号:

public class MyBookingsViewModel
{
    public Cart Carts { get; set; }
    public List<Booking> Bookings { get; set; }
}

public class Cart
{
    [Key]
    public int RecordId { get; set; }
    public string CartId { get; set; }       
    public int Count { get; set; }



    [ForeignKey("Booking")]
    public int BookingId { get; set; }
    public virtual Booking Booking { get; set; }

    [ForeignKey("StatusCode")]
    public int Stage { get; set; }
    public virtual StatusCode StatusCode { get; set; }

    public Cart()
    {
        this.Booking = new List<Booking>();
    }
}

public class Booking
{
     [DisplayName("BookingId")]
    public int BookingId { get; set; }

    [ForeignKey("Item")]
    public int ItemId { get; set; }
    public virtual Item Item { get; set; }

    [DisplayName("Name")]
    public string UserFullName { get; set; }


    [DateStart]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yy hh:mm tt}", ApplyFormatInEditMode = true)]
    public DateTime StartDate { get; set; }

    [DateEnd(DateStartProperty = "StartDate")]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yy hh:mm tt}", ApplyFormatInEditMode = true)]
    public DateTime EndDate { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:d MMM yyyy}", ApplyFormatInEditMode = true)]
    public System.DateTime RequestDate { get; set; }

    [DisplayName("Login")]
    public string Login { get; set; }

    [DisplayName("NetworkID")]
    public string NetworkID { get; set; }

    [DisplayName("Comments")]
    public string Comments { get; set; }

}

查看:

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Carts.RecordId)
        </td>
        <td>
            <a href="@Url.Action("ItemDetails", new {id=item.Carts.BookingId })" >
            @Html.DisplayFor(modelItem => item.Carts.CartId)
                </a>
            @foreach(var booking in item.Bookings)
   {
     <li>@booking.Item.ItemName</li>
   }
        </td>
</tr>
}

1 个答案:

答案 0 :(得分:6)

查看您的模型类定义

public class MyBookingsViewModel
{
    public Cart Carts { get; set; }
    public List<Booking> Bookings { get; set; }
}

以及控制器代码中的以下行

foreach (var item in myBookingList)
{
    MyBookingsViewModel model = new MyBookingsViewModel();

    model.Carts.RecordId = item.RecordId;
    model.Carts.CartId = item.CartId;
    model.Carts.Booking.Item.ItemName = item.Booking.Item.ItemName;
    model.Carts.Booking.UserFullName = item.Booking.UserFullName;
    model.Carts.Booking.RequestDate = item.Booking.RequestDate;
    model.Carts.StatusCode.StatusCodeName = item.StatusCode.StatusCodeName;
    result.Add(model);
}

您尝试设置model.Carts.RecordId,而model.Carts为空。在执行此操作之前,您需要初始化Carts属性。初始化应该在构造函数中完成,以避免代码重复。将模型类定义更改为以下

public class MyBookingsViewModel
{
    public MyBookingsViewModel()
    {
        this.Carts = new Cart();
    }
    public Cart Carts { get; set; }
    public List<Booking> Bookings { get; set; }
}

<强>更新 根据您对此行中错误的评论:

model.Carts.Booking.Item.ItemName = item.Booking.Item.ItemName;

设置model.Carts.Booking的值时,问题model.Carts.Booking.Item为空。这一行也有类似的问题:

model.Carts.StatusCode.StatusCodeName = item.StatusCode.StatusCodeName;
设置model.Carts.StatusCode的值时,

model.Carts.StatusCode.StatusCodeName为null,因此您需要在构造函数中将类似的内容添加到Cart类定义中,如下所示:

public class Cart()
{
    public Cart()
    {
        this.Booking = new Booking();
        this.StatusCode = new StatusCode();
    }
}

以及Booking类定义

public class Booking()
{
    public Booking()
    {
        this.Item = new Item();
    }
}