I'm writing a hotel reservation web site with Asp.net and MVC 4. it have a class named reservation which have a list of contacts. in create view i want to dynamically create contacts. (number of contacts = adults + kids and it will be determined in create reservation view) how could i post contact information to controller?
public partial class Reservation
{
public int Id { get; set; }
public int RoomType_Id { get; set; }
public System.DateTime FromDate { get; set; }
public System.DateTime ToDate { get; set; }
public byte Adults { get; set; }
public byte Kids { get; set; }
public decimal Price { get; set; }
public int User_Id { get; set; }
public int State_Id { get; set; }
public virtual ReservationState ReservationState { get; set; }
public virtual RoomType RoomType { get; set; }
public virtual User User { get; set; }
public virtual ICollection<Transaction> Transactions { get; set; }
public virtual ICollection<Contact> Contacts { get; set; }
}
should i set a maximum number for contacts(for example 5 and then write something like this?
[HttpPost]
public ActionResult Create(Reservation reservation,Contact Adult1,Contact Adult2, Contact Adult3, Contact Adult4, Contact Adult5, Contact Kid1,Contact Kid2, Contact Kid3)
{
if(reservation.Adults>0)
reservation.Contacts.Add(Adult1);
if(reservation.Adults>1)
reservation.Contacts.Add(Adult2);
...
if (ModelState.IsValid)
{
_db.Reservations.Add(reservation);
_db.SaveChanges();
return RedirectToAction("Index");
}
}
it's very dirty is there a better way? can i pass list of contacts?