在 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);
}
尝试将其更改为属性而不是字段:
public List<PersonViewModel> persons {get;set;}
并从构造函数初始化它:
persons = new List<PersonViewModel>();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句