[英]Razor Pages PageModel is Returning Null Property
Why is this razor page returning a null property for Model.People?为什么此 razor 页面返回 Model.People 的 null 属性?
People.cshtml.cs人.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人.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.'发生的情况是调用 OnGet 并填充人员列表,但是当视图被点击时,视图抛出 System.NullReferenceException:'对象引用未设置为 object 的实例。 on the Model.People.关于 Model.People。 People is null for the view.人们是 null 的视图。 I don't understand why it is getting overwritten with a null value after being set in the OnGet.我不明白为什么在 OnGet 中设置后它会被 null 值覆盖。
EDIT Seems like the problem is declaring the type.编辑似乎问题在于声明类型。 I would still like to know why this breaks the model.我仍然想知道为什么这会破坏 model。
List<PeopleObj> People =
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; }
因为你使用List<PeopleObj> People = GetPeople().ToList();//returns 4 people
,你只是创建了一个新列表,你没有初始化[BindProperty]public List<PeopleObj> People { get; set; }
[BindProperty]public List<PeopleObj> People { get; set; }
[BindProperty]public List<PeopleObj> People { get; set; }
. [BindProperty]public List<PeopleObj> People { get; set; }
。 So when you use foreach,the list(Model.People) is null.null cannot be foreach.所以当你使用foreach时,列表(Model.People)是null.null不能是foreach。
Solution:解决方案:
If you want to set the property of PeopleModel.You can change如果你想设置 PeopleModel 的属性。你可以改变
List<PeopleObj> People = GetPeople().ToList();
to至
People = GetPeople().ToList();
And then you can still get the edited data from People in OnPost handler.然后您仍然可以从 OnPost 处理程序中的 People 获取编辑后的数据。
Here is a demo:这是一个演示:
Model: Model:
public class PeopleObj
{
public int Id { get; set; }
public string Name { get; set; }
}
People.cshtml:人.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:人.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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.