.NET 6 Razor 页面部分标记助手 - 模型抛出 'System.NullReferenceException'

.NET 6 Razor Page Partial tag helper - Model throws 'System.NullReferenceException'

我有一个非常简单的局部视图:

@page
@using IlgQueries.Common.Interfaces;
@model IlgQueries.Common.Interfaces.ProsByArea;

<ul class="pros-list">
    @foreach (ProByGeoAreaData pro in Model.Pros)
    {
        <li>@pro.FullName - @Model.ProCategoryName - <strong>@pro.City</strong> @pro.PostalCode</li>
        <li>@pro.FullName - @Model.ProCategoryName - <strong>@pro.City</strong> @pro.PostalCode</li>
    }
</ul>

并且我在我的 Index.cshtml Razor 页面中使用了这个部分视图:


<div class="col-12">
    <h2>@Model.ProsByRegion.TotalItems @(Model.ProsByRegion.ProCategoryName) some text @(Model.ProsByRegion.TotalItems != 1 ? "s" : "") some text</h2>
    
    <partial name="Pros/Shared/_ProsList" for="ProsByRegion"/>
</div>

但我在局部视图中得到NullReferenceException:

Exception thrown: 'System.NullReferenceException' in System.Private.CoreLib.dll

这里:

@foreach (ProByGeoAreaData pro in Model.Pros)
{
   ...code
}

我的索引页模型中确实有一个值(Model.ProsByRegion.Pros 不为空)。

这是索引模型:

public class IndexModel : IlgPageModelBase
{
    public ProsByArea ProsByRegion { get; private set; } = default!;
}

ProsByArea


public record ProsByArea {
  public ProsByArea(string proCategoryName,
   string areaName, uint actualPageNum, uint totalItems, uint totalPages) 
{
    
    if (string.IsNullOrWhiteSpace(proCategoryName))
      throw new ArgumentNullException(nameof(proCategoryName));

    if (string.IsNullOrWhiteSpace(areaName))
      throw new ArgumentNullException(nameof(areaName));

    ProCategoryName = proCategoryName;
    AreaName = areaName;
    Pros = new List < ProByGeoAreaData > (30);
    ActualPageNum = actualPageNum;
    TotalItems = totalItems;
    TotalPages = totalPages;
    
  }

  public uint ActualPageNum { get; }
  public string AreaName { get; }

  public string ProCategoryName { get; }

  public IList<ProByGeoAreaData> Pros { get; internal set; }

  public uint TotalItems { get; }
  public uint TotalPages { get; }

}

为什么我会得到 NRE?

更新

我打开了一个详细的异常视图,这里是 TargetSite 属性值:

{IlgQueries.Common.Interfaces.ProsByArea get_Model()}

为什么get_Model()我自己的模型class上(实际上,记录)? WTF?

将其转换为 ViewComponent - 非常棒!

@page
@using IlgQueries.Common.Interfaces;
@model IlgQueries.Common.Interfaces.ProsByArea;

<ul class="pros-list">
    @foreach (ProByGeoAreaData pro in Model.Pros)
    {
        <li>@pro.FullName - @Model.ProCategoryName - <strong>@pro.City</strong> @pro.PostalCode</li>
        <li>@pro.FullName - @Model.ProCategoryName - <strong>@pro.City</strong> @pro.PostalCode</li>
    }
</ul>

“get_Model()”异常与上述代码在局部视图中有关,您混合了 Razor Pages 和局部视图,在局部视图中,无需添加 @page 指令,尝试从局部视图中删除 @page 指令,并在 Index.cshtml.cs OnGet 方法中设置 Pros 的值。像这样:

    public void OnGet()
    {
        ProsByRegion = new ProsByArea("C1", "Area A", 12, 10, 5)
        {

            Pros = new List<ProByGeoAreaData>()
            {
                new ProByGeoAreaData(){ ID=101, FullName="F1", City="C1", PostalCode="PC1" },
                new ProByGeoAreaData(){ ID=102, FullName="F2", City="C2", PostalCode="PC2" },

            }
        };
    }

像这样的局部视图(将应用程序和模型引用更改为您的):

 @model Core6RazorPage.Data.ProsByArea;

<h2>This is the partial view</h2> 
@if (Model.Pros!=null && Model.Pros.Count > 0)
{
    <ul class="pros-list">
        @foreach (ProByGeoAreaData pro in Model.Pros)
        {
            <li>@pro.FullName - @Model.ProCategoryName - <strong>@pro.City</strong> @pro.PostalCode</li>
        }
    </ul>
}

结果是这样的: