[英]All values in complex parameter in post action are null
This is a simple MVC app which reads and writes to an Excel file. 这是一个简单的MVC应用程序,可以读取和写入Excel文件。 The functionality can be simplify in these three steps.
这三个步骤可以简化功能。
MVC View shows as below MVC视图显示如下
Model 模型
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table class="table" style="align-content:center">
@*<tr>
<th>
@Html.LabelFor(model => model.appointments.FirstOrDefault().Facility)
</th>
<th>
@Html.LabelFor(model => model.appointments.FirstOrDefault().MRNumber)
</th>
<th>
@Html.LabelFor(model => model.appointments.FirstOrDefault().GroupTopic)
</th>*@
@*<th>
@Html.LabelFor(model => model.appointments.FirstOrDefault().Date)
</th>*@
@*<th>
@Html.LabelFor(model => model.appointments.FirstOrDefault().LOC)
</th>
<th>
@Html.LabelFor(model => model.appointments.FirstOrDefault().CheckedIn)
</th>
</tr>*@
@for (int t = 0; t < Model.appointments.Count; t++)
{
<tr>
<td>
@Html.DisplayFor(model => Model.appointments[t].Facility)
</td>
<td>
@Html.DisplayFor(model => Model.appointments[t].MRNumber)
</td>
<td>
@Html.DisplayFor(model => Model.appointments[t].GroupTopic)
</td>
@*<td>
@Html.DisplayFor(model => Model.appointments[t].Date)
</td>*@
<td>
@Html.DisplayFor(model => Model.appointments[t].LOC)
</td>
<td>
@Html.CheckBoxFor(model => Model.appointments[t].CheckedIn)
</td>
</tr>
}
</table>
<input type="submit" name="Submit" value="Submit" class="btn btn-info btn-sm" />
}
View Model 查看模型
public class AppointmentsViewModel { public List<Appointments> appointments { get; set; } }
Index HttpGet passes a View Model Object which shows the 14 rows 索引HttpGet传递了一个View模型对象,该对象显示了14行
public ActionResult Index() { AppointmentsViewModel vm = new AppointmentsViewModel(); //Some code is which populates vm with data from Excel return View(vm); }
Index HttpPost Action is getting a vm with a list of 14 appointment objects but all properties show null 索引HttpPost Action正在获取具有14个约会对象列表的vm,但所有属性均显示为null
[HttpPost] public ActionResult Index(AppointmentsViewModel vm) { //Breakpoint //Some code to update Excel file }
I place a break point at "Break Point" above, looked into what is passed to vm and it shows all the 14 rows but with null properties 我在上面的“断点”处放置一个断点,查看传递给vm的内容,它显示了所有14行,但具有空属性
In my view, I made sure I am using a for loop instead of foreach, and that I am using an @Html.DisplayFor for each property in the model. 在我看来,我确保我使用的是for循环而不是foreach,并且我对模型中的每个属性都使用@ Html.DisplayFor。 Also my model is declared as
另外我的模型被声明为
@model Scheduling_Demo.Models.AppointmentsViewModelCode for view is below 视图的@model Scheduling_Demo.Models.AppointmentsViewModelCode在下面
@using (Html.BeginForm("Index", "Home", FormMethod.Post)) { <table class="table" style="align-content:center"> @*<tr> <th> @Html.LabelFor(model => model.appointments.FirstOrDefault().Facility) </th> <th> @Html.LabelFor(model => model.appointments.FirstOrDefault().MRNumber) </th> <th> @Html.LabelFor(model => model.appointments.FirstOrDefault().GroupTopic) </th>*@ @*<th> @Html.LabelFor(model => model.appointments.FirstOrDefault().Date) </th>*@ @*<th> @Html.LabelFor(model => model.appointments.FirstOrDefault().LOC) </th> <th> @Html.LabelFor(model => model.appointments.FirstOrDefault().CheckedIn) </th> </tr>*@ @for (int t = 0; t < Model.appointments.Count; t++) { <tr> <td> @Html.DisplayFor(model => Model.appointments[t].Facility) </td> <td> @Html.DisplayFor(model => Model.appointments[t].MRNumber) </td> <td> @Html.DisplayFor(model => Model.appointments[t].GroupTopic) </td> @*<td> @Html.DisplayFor(model => Model.appointments[t].Date) </td>*@ <td> @Html.DisplayFor(model => Model.appointments[t].LOC) </td> <td> @Html.CheckBoxFor(model => Model.appointments[t].CheckedIn) </td> </tr> } </table> <input type="submit" name="Submit" value="Submit" class="btn btn-info btn-sm" /> }
Based on the comments above, since the only value that is being passed successfully is that of the @Html.CheckboxFor, I searched why @Html.DisplayFor was not binding the model to the post action. 根据上面的评论,由于成功传递的唯一值是@ Html.CheckboxFor的值,因此我搜索了为什么@ Html.DisplayFor没有将模型绑定到后期操作。 The post linked below answers my question.
下面链接的帖子回答了我的问题。 Thanks everyone.
感谢大家。 Hope this could help someone else.
希望这可以帮助其他人。
MVC4 Razor - @Html.DisplayFor not binding to model MVC4 Razor-@ Html.DisplayFor不绑定到模型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.