samedi 25 avril 2015

When I render a partial view with ajax.actionLink the input values of the partial view does not get posted with the host view


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_ShiftID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("BistroReservations_ShiftID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.BistroReservations_ShiftID, "", 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>
        <br />

        <div class="form-group">
            @Html.LabelFor(model => model.ArrivalTime, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("ArrivalTime", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.ArrivalTime, "", new { @class = "text-danger" })
            </div>
        </div>   

        <div class="form-group">
            @Html.LabelFor(model => model.LocationID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("LocationID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.LocationID, "", new { @class = "text-danger" })
            </div>
        </div>   

        <div class="form-group">
            @Html.LabelFor(model => model.BistroReservations_TypeOfSeatingID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("BistroReservations_TypeOfSeatingID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.BistroReservations_TypeOfSeatingID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.TableNoID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("TableNoID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.TableNoID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.BistroReservations_StatusID, "BistroReservations_StatusID", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("BistroReservations_StatusID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.BistroReservations_StatusID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Comment, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Comment, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Comment, "", new { @class = "text-danger" })

                @*@Ajax.ActionLink("Add Furniture", "Furniture", new AjaxOptions()
           {
               HttpMethod = "GET",
               UpdateTargetId = "Furniture",
               InsertionMode = InsertionMode.InsertAfter
           })*@

            </div>
        </div>



        <div id="Furniture" 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();
        }


Aucun commentaire:

Enregistrer un commentaire