简体   繁体   English

后期操作中复杂参数中的所有值都为空

[英]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. 这三个步骤可以简化功能。

  1. Read list of patients booked for appointment and show them to the user. 阅读预约的患者名单,并向用户显示。
  2. Allow user to check in who came in. 允许用户签入谁。
  3. Update Excel with updated input. 使用更新的输入来更新Excel。
The application reads fine from the Excel sheet, and shows all the records in the view as shown below. 该应用程序可以从Excel工作表中正常读取,并在视图中显示所有记录,如下所示。 However, when submitting, the parameter in the post action (which should be the model passed from the view) gets the 14 rows but shows null values for all properties in the model. 但是,提交时,post操作中的参数(应该是从视图传递的模型)获得14行,但显示模型中所有属性的空值。 Hope the screenshots and code below can help understand this issue. 希望下面的屏幕截图和代码可以帮助您理解此问题。 Thanks for any help/feedback 感谢您的帮助/反馈

Excel looks as below Excel如下图所示 在此处输入图片说明

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM