在初始化视图之前,我将空的ViewModels添加到List。这些ViewModel需要由用户在视图中进行编辑。如果用户输入值并按Submit,则HTTPPost方法中的列表为空。
我已经尝试使用多个@ HTML.HiddenFor()语句。但是该列表一直保持为空。
[HttpGet]
public ActionResult CreateGuestsView(BookingViewModel bookingViewModel)
{
BookingStepTwoViewModel VM = new BookingStepTwoViewModel()
{
AmountOfGuests = bookingViewModel.AmountOfGuests,
Date = bookingViewModel.Date,
HotelRoomId = bookingViewModel.HotelRoomId,
};
VM.InitGuests();
return View(VM);
}
[HttpPost]
public ActionResult CreateGuestsView( BookingStepTwoViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
return RedirectToAction("CreateGuestsView", "Booking", model);
}
ViewModel:
public class BookingStepTwoViewModel
{
public DateTime Date { get; set; }
public List<GuestViewModel> Guests { get; set; }
public int AmountOfGuests { get; set; }
public int HotelRoomId { get; set; }
public void InitGuests()
{
Guests = new List<GuestViewModel>();
for (int i = 0; i < AmountOfGuests; i++)
{
Guests.Add(new GuestViewModel());
}
}
}
视图:
@{
ViewBag.Title = "Voer gasten in";
}
@model HotelDeBotelSite.Models.ViewModels.BookingStepTwoViewModel
<h2>Voer de gegevens over de gasten in</h2>
<div class="container">
<div class="box banner">
@using (@Html.BeginForm("CreateGuestsView", "Booking", Model))
{
// @Html.HiddenFor(m => m.Guests)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
for (int i = 0; i < Model.Guests.Count(); i++)
{
int counter = i + 1;
<div class="page-header">
<h3>Gast @counter </h3>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Guests[i].Name, "Naam")
@Html.EditorFor(m => m.Guests[i].Name)
@Html.ValidationMessageFor(model => model.Guests[i].Name, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Guests[i].PostalCode, "Postcode")
@Html.EditorFor(m => m.Guests[i].PostalCode)
@Html.ValidationMessageFor(model => model.Guests[i].PostalCode, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Guests[i].adress, "Straatnaam")
@Html.EditorFor(m => m.Guests[i].adress)
@Html.ValidationMessageFor(model => model.Guests[i].adress, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Guests[i].housenumber, "Huisnummer")
@Html.EditorFor(m => m.Guests[i].housenumber)
@Html.ValidationMessageFor(model => model.Guests[i].housenumber, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Guests[i].addon, "Toevoeging")
@Html.EditorFor(m => m.Guests[i].addon)
@Html.ValidationMessageFor(model => model.Guests[i].addon, "", new { @class = "text-danger" })
</div>
}
<button type="submit">volgende stap</button>
}
</div>
GuestViewModel类:
public class GuestViewModel
{
private Guest _guest;
[Required(ErrorMessage = "Het opgeven van een naam is vereist")]
public String Name
{
get
{
return _guest.Name;
}
set
{
_guest.Name = value;
}
}
[Required(ErrorMessage = "Het opgeven van een postcode is vereist")]
public String PostalCode
{
get
{
return _guest.PostalCode;
}
set
{
_guest.PostalCode = value;
}
}
[Required(ErrorMessage = "Het opgeven van een straatnaam is vereist")]
public String adress
{
get
{
return _guest.adress;
}
set
{
_guest.adress = value;
}
}
public String addon
{
get
{
return _guest.addon;
}
set
{
_guest.addon = value;
}
}
[Required(ErrorMessage = "Het opgeven van een huisnummer is vereist")]
[Range(1,20,ErrorMessage = "Tussen 1 en 20")]
public int housenumber
{
get
{
return _guest.housenumber;
}
set
{
_guest.housenumber = value;
}
}
public GuestViewModel()
{
_guest = new Guest();
}
}
答案 0 :(得分:0)
我通过删除using(@ html.beginForm)语句中的参数来修复它。
@using (@Html.BeginForm("CreateGuestsView", "Booking", Model))
成为:
@using (@Html.BeginForm())