0

I know there's other questions about this, but I want a better explanation about it.

I want to show a table with my Model.List and after POST I still need to access that list. Today this is not happening.

This is my cshtml:

@foreach (var student in Model.ListSchedulingDetails)
    {
        <tr>
            <td width="150">
                @Html.DisplayFor(modelItem => student.SchedulingDate)
            </td>
            <td width="250">
                @Html.DisplayFor(modelItem => student.TeacherName)
            </td>
            <td width="250">
                @Html.DisplayFor(modelItem => student.StudentName)
            </td>
            <td width="150">
                @Html.DisplayFor(modelItem => student.SchedulingHour)
            </td>
            <td width="250">
                @Html.DisplayFor(modelItem => student.SchedulingObservation)
            </td>
        </tr>
    }

After POST, my Model.ListSchedulingDetails is null. What's happening?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
gregoryp
  • 920
  • 4
  • 15
  • 35
  • 6
    Only input items inside a form are posted back to the model. You can use hidden fields for items that don't have actual inputs. You can also persist with session, viewbag, etc. but the common pattern is to just post back what you intend to update, then if an error occurs reload the lists. – Steve Greene Apr 19 '17 at 14:29
  • 1
    Possible duplicate of [ASP.NET MVC 4 - for loop posts model collection properties but foreach does not](http://stackoverflow.com/questions/14165632/asp-net-mvc-4-for-loop-posts-model-collection-properties-but-foreach-does-not) – Ben Sewards Apr 19 '17 at 14:32

1 Answers1

7

There are two things here:

  1. DisplayFor() is for only displaying. For posting, we need input elements like HiddenFor(),TextBoxFor()

  2. you would need to index them for posting back in case of collections, foreach wouldn't help

Your code would need to be like:

 @for (int i=0; i< Model.ListSchedulingDetails.Count; i++)
 {
     <tr>
        <td width="150">
            @Html.DisplayFor(modelItem => ListSchedulingDetails[i].SchedulingDate)
            @Html.HiddenFor(modelItem => ListSchedulingDetails[i].SchedulingDate)
        </td>
        <td width="250">
            @Html.DisplayFor(modelItem => ListSchedulingDetails[i].TeacherName)
            @Html.HiddenFor(modelItem => ListSchedulingDetails[i].SchedulingDate)
        </td>
        <td width="250">
            @Html.DisplayFor(modelItem => ListSchedulingDetails[i].StudentName)
           @Html.HiddenFor(modelItem => ListSchedulingDetails[i].SchedulingDate)
        </td>
        <td width="150">
           @Html.DisplayFor(modelItem => ListSchedulingDetails[i].SchedulingHour)
           @Html.HiddenFor(modelItem => ListSchedulingDetails[i].SchedulingDate)
        </td>
        <td width="250">
            @Html.DisplayFor(modelItem => ListSchedulingDetails[i].SchedulingObservation)
            @Html.HiddenFor(modelItem => ListSchedulingDetails[i].SchedulingDate)
        </td>
    </tr>
 }
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • 2
    While this is definitely a workable solution, it's not really a great idea. You should never post things just so they exist after the post. The correct method is to rebuild your list after post, by querying it again from a database or however you got it the first time around. If you're concerned about making another query, you can utilize caching for that,. – Chris Pratt Apr 19 '17 at 14:52
  • agreed, question was about posting as `null` so i showed how to post `List` back to controller :) – Ehsan Sajjad Apr 19 '17 at 14:54
  • Understood. My comment was mostly intended for the OP. – Chris Pratt Apr 19 '17 at 14:58
  • @EhsanSajjad than you so much for this explanation. – gregoryp Apr 19 '17 at 15:00
  • @ChrisPratt I was doing exactly that, but I taught I was doing it wrong, so I asked this quesiton. – gregoryp Apr 19 '17 at 15:00
  • Why did you think it was wrong? Regardless, nope. That's exactly how you should do it. – Chris Pratt Apr 19 '17 at 15:01
  • @ChrisPratt I think it was wrong because I was querying the same data I had before the form post. – gregoryp Apr 19 '17 at 15:33
  • I've never found the reloads to be a performance issue, but if you do there are techniques like [this](http://stackoverflow.com/questions/18703958/persist-selectlist-in-model-on-post). – Steve Greene Apr 19 '17 at 15:54
  • @ChrisPratt Not disagreeing, but the problem I run into with that (and don't have a good solution for) is caching gets very complicated very easily. If your query has a lot of parameters that may vary your cache can get huge very quickly. If your site runs in a farm, cache synchronization/invalidation becomes a problem and an expensive one to fix. – xr280xr Dec 22 '18 at 03:12
  • For the life of me, I've never understood while people are so afraid to query a database: that's what they're there for. If the query is slow, you can optimize it: stored procedures, proper indexing, throw more resources at your instance, etc. Caching only makes sense when you're showing the same data over and over *and* the query is complex enough that relying on cache actually saves you something (profile to know for sure). If the data is variable, then just query your database. – Chris Pratt Dec 22 '18 at 10:28