2

I cannot understand why I am getting this error, could you please help me find what I am doing wrong ?

What i am trying to achieve with this ViewModel is because i have many records with the same CartId to use it as a parent and booking.Item.ItemName as a child.

Can you please advise if the way i am trying to do it is correct?

Controller:

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

Model:

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

}

View:

@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>
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Wizeman1986
  • 183
  • 1
  • 4
  • 16

1 Answers1

8

Have a look at your model class definition

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

and the following lines in your controller code

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

You were trying to set model.Carts.RecordId while model.Carts is null. You need to initialize Carts property before doing so. The initialization should be done in the constructor to avoid code repetition. Change your model class definition to the following

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

UPDATE: As per your comment regarding the error in this line:

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

The problem is model.Carts.Booking is null when you set the value of model.Carts.Booking.Item. This line also has a similar problem:

model.Carts.StatusCode.StatusCodeName = item.StatusCode.StatusCodeName;

model.Carts.StatusCode is null when you set the value of model.Carts.StatusCode.StatusCodeName, so you need to add the similar thing to the Cart class definition in the constructor like below:

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

and also in the Booking class definition

public class Booking()
{
    public Booking()
    {
        this.Item = new Item();
    }
}
ekad
  • 14,436
  • 26
  • 44
  • 46
  • Thanks ekad , that did the trick for Carts but now i am getting the same error 2 lines bellow at : model.Carts.Booking.Item.ItemName = item.Booking.Item.ItemName; Tried to do the same for Bookings but didnt work, any ideas? – Wizeman1986 Sep 15 '14 at 15:45
  • What does the `Cart` class definition look like? I guess the problem is similar. – ekad Sep 15 '14 at 15:46
  • What do you mean ? its a simple model class.. – Wizeman1986 Sep 15 '14 at 15:49
  • see the updated answer, I think the problem is `model.Carts.Booking` is null when you set `model.Carts.Booking.ItemName = item.Booking.Item.ItemName` – ekad Sep 15 '14 at 15:56
  • Cannot implicitly convert type 'System.Collections.Generic.List' to 'BookingSystem.Models.Booking' – Wizeman1986 Sep 15 '14 at 16:05
  • i am getting the above error now at the Cart class definition :( – Wizeman1986 Sep 15 '14 at 16:06
  • I think the updated answer will solve your problem. If not, please edit your question and add the class definition of `Cart` and `Booking`. – ekad Sep 15 '14 at 16:07
  • Ok done that , i hope it will help you :) – Wizeman1986 Sep 15 '14 at 16:10
  • Please check the latest updated answer, `this.Booking = new List();` in the constructor should be changed to `this.Booking = new Booking();` – ekad Sep 15 '14 at 16:13
  • Same error at the same place : model.Carts.Booking.Item.ItemName but Hovering over ".Item." is shown as Null this time instead of the ".Booking." – Wizeman1986 Sep 15 '14 at 16:15
  • It's still the same problem, you have to initialize `model.Carts.Booking.Item` before setting its properties. Same thing with `model.Carts.StatusCode`. – ekad Sep 15 '14 at 16:42