0

This is a simple MVC app which reads and writes to an Excel file. 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.
The application reads fine from the Excel sheet, and shows all the records in the view as shown below. 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. Hope the screenshots and code below can help understand this issue. Thanks for any help/feedback

Excel looks as below enter image description here

MVC View shows as below enter image description here


Model

public class Appointments
{
    public string Facility { get; set; }
    public string MRNumber { get; set; }
    public string GroupTopic { get; set; }
    public string LOC { get; set; }
    public bool CheckedIn { get; set; }
}

View Model

public class AppointmentsViewModel
{
    public List<Appointments> appointments { get; set; }
}

Index HttpGet passes a View Model Object which shows the 14 rows

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]
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 enter image description here

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. Also my model is declared as
@model Scheduling_Demo.Models.AppointmentsViewModelCode for view is below

@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" />
}
Yoismel
  • 115
  • 1
  • 10
  • 1
    The problem would be in the data that is sent from the browser to your controller. Open the dev tools (F12) in the browser and look at the body of the message being sent. There is likely just a mistake in it somewhere. Add that data to your question and we can help you. – Gabriel Luci Dec 15 '18 at 04:40
  • 1
    It is getting the `CheckedIn` property though, since that is showing `true`. – Gabriel Luci Dec 15 '18 at 04:42
  • Indeed the CheckedIn is being passed along with any change, but all other properties are still showing null. Looking in how to get the information from the Dev Tools – Yoismel Dec 15 '18 at 19:05
  • Use the "Network" tab of the dev tools to see the request. – Gabriel Luci Dec 15 '18 at 19:31

1 Answers1

0

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. The post linked below answers my question. Thanks everyone. Hope this could help someone else.
MVC4 Razor - @Html.DisplayFor not binding to model

Yoismel
  • 115
  • 1
  • 10