I am having a problem with creating an appropriate ViewModel to allow me to allow user to choose multiple tickets at once in a View.
I have a Ticket model
public class Ticket
{
public int TicketID { get; set; }
[Required]
[ForeignKey("Event")]
//foreign key
public int EventID { get; set; }
[Required]
public string Description { get; set; }
[Required]
public float Price { get; set; }
//navigation property
public virtual Event Event { get; set; }
}
An Order Model
public class Order
{
public int OrderID { get; set; }
//foreign key
[Required]
[ForeignKey("Event")]
public int EventID { get; set; }
[Required]
public DateTime OrderDate { get; set; }
[Required]
public float OrderTotal { get; set; }
public ApplicationUser user { get; set; }
public virtual Event Event { get; set; }
}
Then I have an OrderDetails model
public class OrderDetails
{
[Required]
public int OrderDetailsID { get; set; }
[Required]
//Foreign key
[ForeignKey("Order")]
public int OrderID { get; set; }
[Required]
//Foreign key
[ForeignKey("Ticket")]
public int TicketID { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string email { get; set; }
[Required]
[DataType(DataType.PhoneNumber)]
public int PhoneNumber { get; set; }
//navigation properties
public virtual Order Order { get; set; }
public virtual Ticket Ticket { get; set; }
}
I also created an additional TicketsOrdered model as I don't know where else I can save the quantity of each ticket type a user has selected
public class TicketsOrdered
{
public int OrderID;
public int TicketID;
public int Quantity;
}
I want to create a View which will display the Ticket Description and Price for each Ticket relating to a specific Event then there will also be a dropdown/textbox to allow users to input the quantity of each ticket they require. Then I want to save the TicketID, OrderID and quantity to my TicketsOrdered db table. The problem I am having is that all the data is relating to so many models and I don't know how to bring it all together. I'm at a total loss on how to achieve this, an example of how I want it to look is when you click book tickets on EventBrite. Any help on a ViewModel which will work or how to use my current models in a View would be really helpful thank you.
EDIT Event Model
public class Event
{
public int EventID { get; set; }
[Required]
public String Name { get; set; }
[Required]
public String Location { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime Date { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public String Description { get; set; }
[Required]
[Display(Name = "Total Tickets Available")]
public int TicketsAvailable { get; set; }
//navigation property
public virtual IEnumerable<Order> Order { get; set; }
//navigation property
public virtual IEnumerable<Ticket> Tickets { get; set; }
}
Eventmodel? Does it contain a propertyIEnumerable<Order>?Eventhave also have a collection of differentTickettypes? And is the idea that you want to create anOrderfor anEventwhich displays all the available ticket types for thatEventand allow the user to enter a quantity for each (say they want 2 standard tickets and 2 deluxe tickets)?OrderandOrderDetailssince there seems to be a 1:1 relationship between them