Search code examples
c#asp.net-corerazorrazor-pages.net-5

Razor Pages PageModel is Returning Null Property


Why is this razor page returning a null property for Model.People?

People.cshtml.cs

public class PeopleModel : PageModel
{
    [BindProperty]
    public List<PeopleObj> People { get; set; }

    public void OnGet()
    {
        List<PeopleObj> People = GetPeople().ToList();//returns 4 people
    }
}

People.cshtml

@page
@model MyProject.Pages.PeopleModel

@foreach (var item in Model.People){
//Model.People is null 
}

What happens is the OnGet gets called and the People list populated, but then when the view is hit, the view throws System.NullReferenceException: 'Object reference not set to an instance of an object.' on the Model.People. People is null for the view. I don't understand why it is getting overwritten with a null value after being set in the OnGet.

EDIT Seems like the problem is declaring the type. I would still like to know why this breaks the model.

List<PeopleObj> People =

Solution

  • Because you use List<PeopleObj> People = GetPeople().ToList();//returns 4 people,you just create a new list,you didn't initialize [BindProperty]public List<PeopleObj> People { get; set; }. So when you use foreach,the list(Model.People) is null.null cannot be foreach.

    People: enter image description here

    Solution:

    If you want to set the property of PeopleModel.You can change

    List<PeopleObj> People = GetPeople().ToList();
    

    to

    People = GetPeople().ToList();
    

    And then you can still get the edited data from People in OnPost handler.

    Here is a demo:

    Model:

    public class PeopleObj 
            {
                public int Id { get; set; }
                public string Name { get; set; }
        
            }
    

    People.cshtml:

    <form method="post">
        @{var count = 0;}
        @foreach (var item in Model.People)
        {
            <div class="form-group">
                <label class="control-label">Id</label>
                <input class="form-control" value="@item.Id" name="People[@count].Id"/>
            </div>
            <div class="form-group">
                <label class="control-label">Name</label>
                <input class="form-control" value="@item.Name" name="People[@count].Name" />
    
            </div>
            count++;
        }
        <input type="submit" value="submit" />
    </form>
    

    People.cshtml.cs:

    public class PeopleModel : PageModel
        {
            [BindProperty]
            public List<PeopleObj> People { get; set; }
    
            public void OnGet()
            {
                People = new List<PeopleObj> { 
                    new PeopleObj{  Id=1, Name="person1"},
                    new PeopleObj{  Id=2, Name="person2"},
                    new PeopleObj{  Id=3, Name="person3"},
                    new PeopleObj{  Id=4, Name="person4"}
                };
            }
            public void OnPost()
            {
                //get edited data from People
                List<PeopleObj> EditedPeople = People;
            }
        }
    

    result: enter image description here