В файле cshtml я перебираю список с объектами и привязываю значения к текстовым полям.Вот мое частичное представление.
@model BotelHotel.Models.ViewModels.BookingViewModel
<div>
@using (Html.BeginForm("StepThree", "Book", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.LabelFor(model => model.BookingDate)<br />
@Html.TextBoxFor(model => model.BookingDate, new { @class = "form-control datepicker", placeholder = "Datum Boeking", @readonly = true })
@Html.LabelFor(model => model.AmountOfPersons)<br />
@Html.TextBoxFor(model => model.AmountOfPersons, new { @class = "form-control", @readonly = true })<br />
for (int i = 0; i < Model.persons.Count; i++)
{
@Html.LabelFor(model => model.persons[i].Name)<br />
@Html.TextBoxFor(model => model.persons[i].Name, new { @class = "form-control"})<br />
}
<button class="btn btn-success">Submit</button>
}
</div>
После нажатия кнопки отправки у меня появляется точка останова в функции StepThree.Список persons
пуст.Есть ли причина, почему он пуст?Остальная часть stackoverflow, кажется, говорит, что это хорошее решение.Но это не работает для меня.
Вот мои модели.
public class BookingViewModel
{
public string step { get; set; }
public int roomID { get; set; }
public List<PersonViewModel> persons = new List<PersonViewModel>();
public BookingViewModel()
{
}
public BookingViewModel(int roomID)
{
this.roomID = roomID;
this.step = "PartialOne";
}
[Required]
[DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
[Display(Name = "Datum")]
public DateTime BookingDate { get; set; }
[Required]
[Display(Name = "Aantal personen")]
public int AmountOfPersons { get; set; }
}
public class PersonViewModel
{
private Person _person;
public PersonViewModel()
{
_person = new Person();
}
public PersonViewModel(Person person)
{
_person = person;
}
public int Id { get; set; }
[Required]
[Display(Name = "Naam")]
public string Name { get; set; }
[Required]
[Display(Name = "Adres")]
public string Address { get; set; }
[Required]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[Display(Name = "Post Code")]
public string Postal_code { get; set; }
}
И метод, который публикуется на
public ViewResult StepThree(BookingViewModel model)
{
model.step = "PartialThree";
return View("Book", model);
}