Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NET MVC中将多个表单从视图传递到控制器_C#_Asp.net Mvc - Fatal编程技术网

C# 在ASP.NET MVC中将多个表单从视图传递到控制器

C# 在ASP.NET MVC中将多个表单从视图传递到控制器,c#,asp.net-mvc,C#,Asp.net Mvc,我正在创建一个在酒店房间预订客人的应用程序。在HttpGet中,我将ReservationViewModel从控制器传递到视图。此VM包含所有reservationdetails和2个空Guest对象(或任意多个)。对于每个来宾对象,我显示一个表单,用户需要在其中输入有关来宾的信息。然而,当我尝试提交时,它只返回一位客人的信息。我试图寻找一种方法来传递一个数组或多个来宾,但这似乎不可能,只发送一个带有参数的来宾对象似乎是可行的 以下是我的GET的代码: [HttpGet] public

我正在创建一个在酒店房间预订客人的应用程序。在HttpGet中,我将ReservationViewModel从控制器传递到视图。此VM包含所有reservationdetails和2个空Guest对象(或任意多个)。对于每个来宾对象,我显示一个表单,用户需要在其中输入有关来宾的信息。然而,当我尝试提交时,它只返回一位客人的信息。我试图寻找一种方法来传递一个数组或多个来宾,但这似乎不可能,只发送一个带有参数的来宾对象似乎是可行的

以下是我的GET的代码:

[HttpGet]
    public ActionResult Edit2(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Reservation reservation = resRepo.GetReservationByID(id);
        ReservationVM reservationVM = new ReservationVM(0);

        for (int i = 0; i < reservation.amount_people; i++)
        {
            reservationVM.guests.Add(new Guest());
        }

        foreach(Guest guest in reservationVM.guests)
        {
            guest.name = " ";
            guest.zipcode = " ";
            guest.housenumber = 0;
            guest.suffix = "";
            guest.email = " ";
            guestRepo.AddGuest(guest);
        }
        guestRepo.Save();

        reservationVM.date = (DateTime)reservation.date;
        reservationVM.amount_people = (int)reservation.amount_people;
        reservationVM.ID = reservation.ID;
        reservationVM.room_ID = (int)reservation.room_ID;

        if (reservation == null)
        {
            return HttpNotFound();
        }
        return View(reservationVM);
    }
以及我的填写表格:

现在我想知道如何将所有forminfo传递给我的控制器,并使用这些信息来保存预订和每个客人,这样我就可以将它们写入我的数据库

谢谢

编辑:我尝试了给出的解决方案,但我似乎无法在我的数据库中获取客户信息。问题似乎是,当它到达foreach循环以使来宾离开reservationVM时,它是空的。尝试在输出进入循环时写入调试输出,但从未写入。这是我的密码

[HttpPost]
    public ActionResult Edit2(ReservationVM reservationVM)
    {
        if (ModelState.IsValid)
        {
            //Get reservation
            Reservation reservation = resRepo.GetReservationByID(reservationVM.ID);
            //Update values in model 
            reservation.date = reservationVM.date;
            reservation.amount_people = reservationVM.amount_people;
            reservation.ID = reservationVM.ID;
            reservation.room_ID = reservationVM.room_ID;
            //Update to DB and save changes
            resRepo.UpdateReservation(reservation);
            resRepo.Save();

            foreach (Guest guest in reservationVM.guests)
            {
                Guest temp = guestRepo.GetGuestByID(guest.ID);

                temp.name = guest.name;
                temp.zipcode = guest.zipcode;
                temp.housenumber = guest.housenumber;
                temp.suffix = guest.suffix;
                temp.email = guest.email;
                temp.ID = guest.ID;

                guestRepo.UpdateGuest(temp);
                reservation.Guests.Add(temp);
            }
            guestRepo.Save();

            //Reservation r = new Reservation { ID = reservation.ID };
            //db.Reservations.Add(r);
            //db.Reservations.Attach(r);

            //Guest g = new Guest { ID = guest.ID };
            //db.Guests.Add(g);
            //db.Guests.Attach(g);

            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(reservationVM);
    }

您正在将
ReservationVM
从view传递到post方法。因此,请按如下方式编写您的帖子:

[HttpPost]
public ActionResult Edit2(ReservationVM reservationVM)
{
    if (ModelState.IsValid)
    {
        // Here access the necessary values you need from `reservationVM` and do your necessary stuffs

        return RedirectToAction("Index");
    }
    return View(reservation);
}

要在post操作中获取来宾信息,您需要按如下方式设置来宾字段:

@for(var i = 0; i < Model.guests.count; i++)
{
   Your editor will be like below 

    @Html.EditorFor(model => model.guests[i].ID)

    @Html.EditorFor(model => model.guests[i].name)
}
for(var i=0;imodel.guests[i].ID) @Html.EditorFor(model=>model.guests[i].name) }
尝试在razor视图中创建与上述代码类似的字段。。会有用的。

欢迎来到StackOverflow。我只是想说你在写第一个问题上做得很好——希望你能找到答案并享受这个社区@卢克·波克,需要看看你的观点!是的,贴了!在图片中,您错过了图片中视图的
部分(使用名称空间
)。使用名称空间包含
部分我的坏。。编辑。哦,那似乎很容易。。我一会儿就试试。我会让你知道的。谢谢嘿,数据库里还没有我的客人。。。下面是一个代码
@for(var i=0;i<@Model.guests.Count;i++){@Html.ValidationSummary(true,“,new{@class=“text danger”})@Html.LabelFor(Model=>Model.guests[i].ID,“ID:,htmlAttributes:new{@class=“control label col-md-2”})的示例@Html.EditorFor(model=>model.guests[i].ID,new{htmlAttributes=new{@class=“form control”}})
那段代码的格式很糟糕,希望你能理解我做了什么。
@for(var i = 0; i < Model.guests.count; i++)
{
   Your editor will be like below 

    @Html.EditorFor(model => model.guests[i].ID)

    @Html.EditorFor(model => model.guests[i].name)
}