简体   繁体   English

Razor 页面 PageModel 正在返回 Null 属性

[英]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。

People:人们: 在此处输入图像描述

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;
        }
    }

result:结果: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 剃刀页面PageModel验证不起作用 - Razor pages PageModel validation not working Razor 页面中 PageModel 与 ViewData 的区别和优势 - Differences and benefits of PageModel vs ViewData in Razor Pages ASP.NET 核心 Razor 页面 - 复杂 model 属性是 Z37A6259CC0C1DAE29BDZA7866489DFF0 后返回页面() - ASP.NET Core Razor Pages - Complex model property is null after returning Page() 简单的注入器注入PageModel ASP.NET核心Razor页面 - Simple Injector Inject into PageModel ASP.NET Core Razor Pages 覆盖 PageModel() 构造函数以设置默认属性(Razor 页面) - Overriding the PageModel() constructer for setting up default properties (Razor Pages) Net Core 2.0 将数据从 IPageFilter 属性传递到 Razor Pages PageModel - Net Core 2.0 pass data from IPageFilter Attribute to Razor Pages PageModel 如何将 PageModel 添加到 Razor 组件? - How to add PageModel to Razor component? 如何在 PageModel 中为表单分配属性(按按钮),维护选定的页面布局 [ASP.NET/Razor 页面] - How to assign property in PageModel (by button) for form, maintaining selected page layout [ASP.NET/Razor Page] ASP.NET 内核 Razor PageModel ModelState 无效,因为新的导航属性 Id==0 - ASP.NET Core Razor PageModel ModelState Invalid because of new Navigation Property with Id==0 剃刀页面-Post上的RouteData为null - Razor pages - RouteData null on Post
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM