C# 提交表单后,模型中的对象列表为空
在cshtml文件中,我迭代一个包含对象的列表,并将值绑定到文本框。这是我的部分观点C# 提交表单后,模型中的对象列表为空,c#,asp.net-mvc,C#,Asp.net Mvc,在cshtml文件中,我迭代一个包含对象的列表,并将值绑定到文本框。这是我的部分观点 @model BotelHotel.Models.ViewModels.BookingViewModel <div> @using (Html.BeginForm("StepThree", "Book", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.LabelFor(model
@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>
公共类PersonViewModel
{
私人(私人),;
公众视角模型
{
_人员=新人员;
}
公众人物视图模型人物
{
_人=人;
}
公共int Id{get;set;}
[必需]
[DisplayName=Naam]
公共字符串名称{get;set;}
[必需]
[显示名称=地址]
公共字符串地址{get;set;}
[必需]
[显示名称=电子邮件]
公共字符串电子邮件{get;set;}
[必需]
[显示名称=邮政编码]
公共字符串邮政编码{get;set;}
}
以及正在发布到的方法
public ViewResult StepThree(BookingViewModel model)
{
model.step = "PartialThree";
return View("Book", model);
}
尝试将其更改为属性而不是字段:
public List<PersonViewModel> persons {get;set;}
然后从构造函数初始化它:
persons = new List<PersonViewModel>();
谢谢作品我会在几分钟内把它作为解决方案。