How to post back a List<> using asp.net razor pages

Viewed 19

I have a written a sample program that shows how I create a Generic LIST and I send that to the VIEW and the VIEW renders the values correctly but when I do a POST the LIST is empty. So Console.WriteLine(TimeEntry.Count) is 0, was kind of expecting 2

Output from execution

public class TimeEntry
{
    public DateTime entryTime { get; set; }
}
public class IndexModel : PageModel
{

    [BindProperty(SupportsGet = true)]
    public List<TimeEntry> TimeEntry { get; set; }
    public void OnGet(List<TimeEntry> timeEntry)
    {
        TimeEntry _timeEntry = new TimeEntry();
        
        _timeEntry.entryTime = DateTime.Now;
        timeEntry.Add(_timeEntry);

        _timeEntry = new TimeEntry();
        _timeEntry.entryTime = DateTime.Now.AddMinutes(10);
        timeEntry.Add(_timeEntry);


        TimeEntry = timeEntry;
    }

    public void OnPost(List<TimeEntry> TimeEntry)
    {
        Console.WriteLine(TimeEntry.Count);
    }
}

above is the server side script

@model IndexModel
@{
    ViewData["Title"] = "Home page";
}
<form class="form-horizontal" method="post">


    <table>
        @if (Model.TimeEntry != null)
        {
            @foreach (var _time in Model.TimeEntry)
            {
                <tr>
                    <td>
                        Entry Time: @_time.entryTime
                    </td>
                </tr>
            }
        }
    </table>

    <button type="submit">Post</button>

</form>
1 Answers
Related