I have search an answer for more than two days now, please can somebody help.
I have a "create" view where reservations can be made for a restaurant. On the view you have to be able to either create a new guest of select an existing guest. The view by default comes where you can select an existing guest, if the guest is not there then you can click on a ajax.actionlink which render a partial view to create a new guest. After you have either selected a current guest or fill in the fields for the new guest you will continue with the reservation. At the end you click create.
My goal is then that the fields of the partial view together with the reservation fields(host view) gets posted to the "create" controller, but the problem is the model binder does not bind the input field of the partial view, so I can't save the new guest together with the new reservation to the database at once.I'm using asp.net mvc5
Main "Create" View
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>BistroReservations_Reservation</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.DateOfArrival, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DateOfArrival, new { htmlAttributes = new { @class = "form-control datecontrol" } })
@Html.ValidationMessageFor(model => model.DateOfArrival, "", new { @class = "text-danger" })
</div>
</div>
...
<div class="form-group">
@Html.LabelFor(model => model.BistroReservations_GuestID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-6">
@Html.DropDownList("BistroReservations_GuestID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.BistroReservations_GuestID, "", new { @class = "text-danger " })
@Ajax.ActionLink("Create a New Guest", "NewGuest", new AjaxOptions()
{
HttpMethod = "GET",
UpdateTargetId = "NewGuest",
InsertionMode = InsertionMode.ReplaceWith,
})
</div>
</div>
<div id="NewGuest" class="form-group">
</div>
...
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
Partial View
@using (Html.BeginForm())
{
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.MobileNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MobileNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MobileNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
</div>
<hr />
}
Create Action Method
[HttpPost]
[ValidateAntiForgeryToken]
[ActionName("Create")]
public ActionResult Create_Post(BistroReservations_Reservation reservation, BistroReservations_Guest guest)
{
if (ModelState.IsValid)
{
db.BistroReservations_Reservations.Add(reservation);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.BistroReservations_GuestID = new SelectList(db.BistroReservations_Guests, "BistroReservations_GuestID", "FirstName");
ViewBag.BistroReservations_ShiftID = new SelectList(db.BistroReservations_Shifts, "BistroReservations_ShiftID", "ShiftDescription");
ViewBag.BistroReservations_StatusID = new SelectList(db.BistroReservations_Statuses, "BistroReservations_StatusID", "StatusDescription");
ViewBag.BistroReservations_TypeOfSeatingID = new SelectList(db.BistroReservations_TypeOfSeatings, "BistroReservations_TypeOfSeatingID", "TypeOfSeating");
ViewBag.ArrivalTime = new SelectList(db.BistroReservations_Times, "BistroReservations_TimeID", "Time");
ViewBag.DepartureTime = new SelectList(db.BistroReservations_Times, "BistroReservations_TimeID", "Time");
ViewBag.TableNoID = new SelectList(db.BistroReservations_TableNumbers, "BistroReservations_TableNoID", "Number");
ViewBag.LocationID = new SelectList(db.BistroReservations_Locations, "BistroReservations_LocationID", "Description");
return View();
}
Model for my Main View
public partial class BistroReservations_Reservation
{
public int BistroReservations_ReservationID { get; set; }
[Display(Name="Day Of Reservations")]
[Required]
[DataType(DataType.Date)]
public DateTime DateOfArrival { get; set; }
public int BistroReservations_ShiftID { get; set; }
public BistroReservations_Shift BistroReservations_Shift { get; set; }
public int BistroReservations_GuestID { get; set; }
public virtual BistroReservations_Guest BistroReservations_Guest { get; set; }
[Required]
[Display(Name = "Arrival Time")]
public int BistroReservations_TimeID { get; set; }
public virtual BistroReservations_Time ArrivalTime { get; set; }
[Required]
[Display(Name="Departure Time")]
public virtual BistroReservations_Time DepartureTime { get; set; }
[Display(Name="Location")]
public int LocationID { get; set; }
public virtual BistroReservations_Location BistroReservations_Location { get; set; }
[Display(Name="Type Of Seating")]
public int BistroReservations_TypeOfSeatingID { get; set; }
public virtual BistroReservations_TypeOfSeating BistroReservations_TypeOfSeating { get; set; }
[Display(Name="Table Number")]
public int TableNoID { get; set; }
public virtual BistroReservations_TableNo BistroReservations_TableNo { get; set; }
[Display(Name="Status")]
public int BistroReservations_StatusID { get; set; }
public virtual BistroReservations_Status BistroReservations_Status { get; set; }
public virtual BistroReservations_ArrivalStatus BistroReservations_ArrivalStatus { get; set; }
[Display(Name="Comments")]
public string Comment { get; set; }
public DateTime DateAdded { get; set; }
public string UserID { get; set; }
public virtual User User { get; set; }
public virtual List<BistroReservations_ReservationFurniture> BistroReservations_ReservationFurniture { get; set; }
Model for my Partial View
public class BistroReservations_Guest
{
public int BistroReservations_GuestID { get; set; }
[Display(Name = "Mobile Number")]
public string MobileNumber { get; set; }
[Required]
[Display(Name="First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
[Display(Name="E-mail Address")]
[EmailAddress]
public string Email { get; set; }
public virtual List<BistroReservations_Reservation> BistroReservations_Reservation { get; set; }
}