gpt4 book ai didi

c# - 提交后ID设置为0

转载 作者:行者123 更新时间:2023-11-30 15:14:26 25 4
gpt4 key购买 nike

我知道有大量示例可以解决我的问题。我已尽我所能,但仍无法解决我的问题。我有一个 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");
}

下面是 Razor 表单的代码:

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

关于c# - 提交后ID设置为0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54723623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com