0

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; }
}
  • Question without the `code` is headache! – Sumit Apr 25 '15 at 20:14
  • I'm very sorry, I added it now, I'm very new to this, please forgive. – Philip Harmse Apr 25 '15 at 20:28
  • Do you declare a `@model` for your main view and partial view? If so what is it? – Jasen Apr 25 '15 at 21:24
  • @Jason, yes I have the models for both, I updated the main question with both. Thank. – Philip Harmse Apr 25 '15 at 21:40
  • One problem I see is you are nesting forms: one for the main view and the other in the partial view. You should remove the `BeginForm` for the partial. – Jasen Apr 25 '15 at 22:00
  • Also, you should post one object to your create action with Guest being a child property of Reservation. – Jasen Apr 25 '15 at 22:05
  • @Jason, regarding removing the BeginForm it worked, thanks so much for that. Regarding having the guest as a child property, I understand what you mean but how would I do that, what if the user selects a current guest how would that work? – Philip Harmse Apr 26 '15 at 08:21
  • @Jason, could you kindly have a look at this question as well, http://stackoverflow.com/q/29875633/4832762?sem=2 it is of the same system just at the bottom of the form the user can add all the furniture to the reservation. – Philip Harmse Apr 26 '15 at 09:03
  • What you should be doing - render the "new guest" form in a dialog, then submit it using ajax, save the guest and in the success function, add the new guest to the dropdown and select it. But if you want to make your implementation work, then remove `@using (Html.BeginForm())` and `@Html.ValidationSummary()` from the partial view (nested forms are invalid and not supported and will not post back). You will also need to re-parse the validator if you want client side validation for the `BistroReservations_Guest` properties –  Apr 26 '15 at 09:55
  • @StephenMuecke do you perhaps have a reference on how to re-parse the validator, I search but couldn't find something that worked for me. Thanks. – Philip Harmse Apr 27 '15 at 08:45
  • Refer [Validate dynamically added fields](http://stackoverflow.com/questions/26542509/validate-dynamically-added-fields/26542591#26542591). But the better approach is as per the first part of my comment above. –  Apr 27 '15 at 08:48

0 Answers0