部分视图的Ajax.actionLink输入值不会随主机视图

时间:2015-04-25 20:12:18

标签: jquery ajax asp.net-mvc

我已经搜索了两天以上的答案,请有人帮忙。

我有一个“创建”视图,可以为餐厅预订。在视图上,您​​必须能够创建选择现有来宾的新来宾。默认情况下,视图位于您可以选择现有guest虚拟机的位置,如果guest虚拟机不存在,则可以单击ajax.actionlink以呈现部分视图以创建新guest虚拟机。选择当前访客或填写新访客的字段后,您将继续预订。最后,单击“创建”。

我的目标是将局部视图的字段与预留字段(主机视图)一起发布到“创建”控制器,但问题是模型绑定器不绑定局部视图的输入字段,所以我无法立即将新客人与新预订一起保存到数据库中。我正在使用asp.net mvc5

主要“创建”视图

@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>
}

部分视图

@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 />    
   }   

创建操作方法

[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();
}

主视图模型

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; }

我的部分视图模型

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; }
}

0 个答案:

没有答案