我知道有很多例子可以解决我的问题。我已尽我所能,但仍然无法解决我的问题。我有一个HotelRoomID,每次提交后都将其设置为0。我使用HotelRoomID从数据库检索数据。
这是我的BookingViewModel
(忽略其他属性)
public int HotelRoomID { get; set; }
public HotelRoom HotelRoom { get; set; }
这是我的
BookingController
。看起来很奇怪,但我的形式是步进形式。[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Stepper(BookingViewModel bookingViewModel) //problem starts here. HotelRoomID is 0...
{
if (!ModelState.IsValid)
{
switch (bookingViewModel.Step)
{
case 1:
GenerateForm(bookingViewModel);
_bookingViewModel.Step = 2;
break;
case 2:
var item = bookingViewModel.PersonList;
GenerateForm(bookingViewModel);
GenerateOverview(bookingViewModel);
break;
case 3:
GenerateOverview(bookingViewModel);
break;
}
return View("Create",_bookingViewModel);
}
SaveBooking(_bookingViewModel);
return View("home");
}
以下是剃须刀形式的代码:
@model HoteldeBotel.Models.BookingViewModel
<div class="row setup-content" id="step-2">
@using (Html.BeginForm("Stepper", "Booking", Model))
{
@Html.AntiForgeryToken()
@if (Model.PersonList != null)
{
<div class="col-xs-12">
<div class="col-md-12 formContent">
<h3> Persoonsgegevens invullen </h3>
@for (int i = 0; i < Model.PersonList.Count(); i++)
{
<ul class="col-md-4 personFormList">
@Html.ValidationMessageFor(model => model.PersonList[i].Name, "", new { @class = "text-danger" })
<li>
@Html.LabelFor(model => Model.PersonList[i].Name, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => Model.PersonList[i].Name, "", new { @class = "form-control numberOfPersons" })
</li>
@Html.ValidationMessageFor(model => model.PersonList[i].Email, "", new { @class = "text-danger" })
<li>
@Html.LabelFor(model => Model.PersonList[i].Email, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => Model.PersonList[i].Email, "", new { @class = "form-control numberOfPersons" })
</li>
<li>
@Html.ValidationMessageFor(model => model.AddressList[i].Name, "", new { @class = "text-danger" })
<br />
<label class="control-label">Straatnaam</label>
@Html.EditorFor(model => Model.AddressList[i].Name, "", new { @class = "form-control numberOfPersons" })
</li>
@Html.ValidationMessageFor(model => model.AddressList[i].ZipCode, "", new { @class = "text-danger" })
<li>
@Html.LabelFor(model => Model.AddressList[i].ZipCode, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => Model.AddressList[i].ZipCode, "", new { @class = "form-control numberOfPersons" })
</li>
@Html.ValidationMessageFor(model => model.AddressList[i].City, "", new { @class = "text-danger" })
<li>
@Html.LabelFor(model => Model.AddressList[i].City, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => Model.AddressList[i].City, "", new { @class = "form-control numberOfPersons" })
</li>
</ul>
}
@if (Model.Step == 2)
{
Model.Step = 3;
<button class="btn btn-primary nextBtn btn-lg pull-right" type="submit">Next</button>
}
else
{
<button class="btn btn-primary nextBtn btn-lg pull-right" type="button">Next</button>
}
</div>
</div>
}
}
</div>
我希望有人可以帮助我,因为我无法解决此问题。
有谁知道如何解决这一问题?
提前致谢。
最佳答案
您缺少HotelRoomID
的隐藏输入字段,如下所示:
@Html.HiddenFor(model => model.HotelRoomID, new { Value = "yourValue"})
此外,将
@using (Html.BeginForm("Stepper", "Booking", Model))
更改为@using (Html.BeginForm("Stepper", "Booking", FormMethod.Post))