You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core 7 Razor Pages:视图空引用异常且OnGetAsync未触发

问题诊断与解决方案

核心问题是视图绑定的模型类型错误,导致Razor Pages框架无法关联到正确的PageModel,OnGetAsync方法完全未执行,进而引发视图层空引用异常。

具体错误点

你的视图中@model ManageSectionVM绑定的是数据模型类,但Razor Pages的视图必须绑定到继承自PageModel的类(即你的ManageSectionModel)。框架找不到对应的PageModel时,会直接渲染视图,此时Model为null,自然触发Model.Sections的空引用,且OnGetAsync的断点永远不会命中。

修复步骤

1. 修改视图的模型绑定声明

将视图顶部的@model改为对应的PageModel类型:

@page
@model FFInfo.AdminUI.Pages.Sections.ManageSectionModel

如果视图已导入命名空间FFInfo.AdminUI.Pages.Sections,可简化为:

@page
@model ManageSectionModel

2. 调整视图中属性的引用路径

现在视图的Model是ManageSectionModel实例,所有UI绑定需要指向它的ManageSectionVM属性:

<h2 class="Title">
    Manage Section Information
</h2>

<form id="Form" enctype="multipart/form-data" method="post" autocomplete="off">
    <p>
        <label asp-for="ManageSectionVM.SelectedSectionID">Update Existing Section?</label>

        <select asp-for="ManageSectionVM.SelectedSectionID" asp-items="Model.ManageSectionVM.Sections">
            <option value="0">Add New Section</option>
        </select>
    </p>
</form>

3. 额外检查点

  • 确认视图文件命名符合约定:ManageSection.cshtml(与PageModel类名ManageSectionModel对应,去掉后缀Model),且放在Pages/Sections目录下,确保路由能正确匹配。
  • 确认调试模式开启:检查launchSettings.json中ASPNETCORE_ENVIRONMENT是否设置为Development,保证断点能正常触发。
  • 后续修复后若数据库调用异常,再确认ISectionHelper是否已在DI容器中正确注册(比如在Program.cs中添加services.AddScoped<ISectionHelper, SectionHelper>())。

确认PageModel代码(无需改动)

你的C# PageModel代码本身无问题,修复视图绑定后OnGetAsync会正常执行,ManageSectionVM的属性会被正确填充:

using FFInfo.AdminUI.Models;
using FFInfo.BLL.Admin.Interfaces;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace FFInfo.AdminUI.Pages.Sections
{
    public class ManageSectionModel : PageModel
    {
        private readonly ISectionHelper _sectionHelper;

        public ManageSectionModel(ISectionHelper sectionHelper)
        {
            _sectionHelper = sectionHelper;
        }

        [BindProperty]
        public ManageSectionVM ManageSectionVM { get; private set; } = new ManageSectionVM();

        public async Task<IActionResult> OnGetAsync()
        {
            ViewData["Title"] = "Manage Section";
            var sections = await _sectionHelper.GetSectionList();
            ManageSectionVM.Sections = new SelectList(sections, "ID", "Name");
            ManageSectionVM.Synopsis = "Sorry, we do not have a synopsis available at this time.";
            return Page();
        }

        public async Task<IActionResult> OnPostAsync()
        {
            return Page();
        }

        public async Task<JsonResult> OnPostGetBasicSectionInfoAsync(int sectionID)
        {
            return new JsonResult(await _sectionHelper.GetBasicSectionInfo(sectionID));
        }
    }
}

内容的提问来源于stack exchange,提问作者Matthew Verstraete

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 22:13:08