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

Razor Pages页面传值异常:Owner到Pet跳转后Pet到Cert页面报错

问题排查:Asp.Net Razor Pages 页面传值异常问题

项目背景

开发的Asp.Net Razor Pages应用用于查看狂犬病疫苗历史信息,核心页面包括:

  • 所有者信息页:展示当前所有者的宠物列表
  • 宠物详情页:展示对应宠物的疫苗接种记录
  • 狂犬病证书打印页:展示特定宠物与疫苗组合的可打印证书

问题现象

此前宠物详情页到证书页的传值功能正常,添加所有者页到宠物详情页的传值逻辑后,宠物详情页中ViewData["petid1"] = Pet.PetId;代码抛出异常。所有者页的跳转按钮hover时能显示正确的OwnerId和PetId参数,但点击跳转后触发错误。

相关代码片段

所有者页跳转按钮视图代码

<!-- 示例代码,实际以用户实现为准 -->
@foreach (var pet in Model.OwnerPets)
{
    <button type="button" onclick="location.href='/PetDetails?ownerId=@Model.OwnerId&petId=@pet.PetId'">
        查看@pet.Name详情
    </button>
}

宠物详情页PageModel代码(异常位置)

public class PetDetailsModel : PageModel
{
    private readonly IRepository _repo;

    public PetDetailsModel(IRepository repo)
    {
        _repo = repo;
    }

    public Pet Pet { get; set; }
    public List<VaccineRecord> VaccineRecords { get; set; }

    public void OnGet(int ownerId, int petId)
    {
        // 异常触发点:Pet未初始化时访问Pet.PetId
        ViewData["petid1"] = Pet.PetId;
        VaccineRecords = _repo.GetVaccineRecordsByPetId(petId);
    }
}

宠物详情页返回按钮代码

<a asp-page="/OwnerDetails" asp-route-ownerId="@ViewData["ownerId"]" class="btn btn-secondary">返回所有者页</a>

狂犬病证书页PageModel更新代码

public class RabiesCertificateModel : PageModel
{
    private readonly IRepository _repo;

    public RabiesCertificateModel(IRepository repo)
    {
        _repo = repo;
    }

    public Pet Pet { get; set; }
    public VaccineRecord VaccineRecord { get; set; }

    public void OnGet(int petId, int vaccineId)
    {
        Pet = _repo.GetPetById(petId);
        VaccineRecord = _repo.GetVaccineRecordById(vaccineId);
    }
}

问题原因与解决方案

核心原因

异常的直接原因是Pet对象未被初始化就访问其PetId属性。从所有者页跳转至宠物详情页时,PageModel的OnGet方法中未先根据传入的petId查询获取Pet对象,直接执行ViewData["petid1"] = Pet.PetId;,此时Pet为null,触发空引用异常。

修复步骤

  1. 修正宠物详情页OnGet方法逻辑
    在访问Pet属性前,先通过传入的petId从数据源获取Pet对象,增加null校验:

    public IActionResult OnGet(int ownerId, int petId)
    {
        // 先查询获取Pet对象
        Pet = _repo.GetPetById(petId);
        if (Pet == null)
        {
            return NotFound("未找到指定宠物");
        }
        // 再赋值ViewData
        ViewData["petid1"] = Pet.PetId;
        VaccineRecords = _repo.GetVaccineRecordsByPetId(petId);
        // 传递ownerId到视图用于返回按钮
        ViewData["ownerId"] = ownerId;
        return Page();
    }
    
  2. 确保参数接收的正确性
    若使用绑定属性接收参数,需添加[BindProperty(SupportsGet = true)]标记(Get请求需显式开启支持):

    public class PetDetailsModel : PageModel
    {
        [BindProperty(SupportsGet = true)]
        public int OwnerId { get; set; }
        
        [BindProperty(SupportsGet = true)]
        public int PetId { get; set; }
        
        // 其他属性与方法...
    }
    
  3. 验证所有者页跳转参数的一致性
    确保跳转链接的参数名与宠物详情页接收的参数名完全匹配(大小写敏感),推荐使用Razor Pages的路由标签助手避免拼写错误:

    @foreach (var pet in Model.OwnerPets)
    {
        <a asp-page="/PetDetails" asp-route-ownerId="@Model.OwnerId" asp-route-petId="@pet.PetId" class="btn btn-primary">
            查看@pet.Name详情
        </a>
    }
    
  4. 调试验证
    在宠物详情页的OnGet方法开头添加断点,检查传入的petId是否正确,以及查询返回的Pet对象是否为null,确认数据流程正常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 00:54:53