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

Razor局部视图无法传递参数,空引用异常求助

动态按钮行空引用异常解决思路

我尝试实现动态按钮行功能,在视图中声明要渲染的按钮,通过Razor局部视图根据传入参数动态渲染,但始终触发空引用异常,相关代码及异常信息如下:

基础视图代码

@page
@using MyPages.Pages.Shared.components
@model MyPages.Pages.Waste.ContainerChallenge.IndexModel
@{
    const string title = "Title for page";
  
}
<div class="col-9 justify-content-center mx-auto mb-4">
  
    @Html.Partial("Shared/components/ButtonRow",new ButtonRowModel(){Button = ButtonRowModel.ButtonType.Start, TargetURL = "/YourPropertyDetails"}) 
</div>

ButtonRowModel代码

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace MyPages.Pages.Shared.components
{
    public class ButtonRowModel : PageModel
    {
        public void OnGet()
        {
        }

        public enum ButtonType
        {
            Start,
            Next,
            Submit
        }

        public ButtonType Button { get; set; }
        public string TargetURL { get; set; }
        public string? PreviousURL { get; set; }
    }
}

局部视图代码

@page
@model MyPages.Pages.Shared.components.ButtonRowModel
@{}

@if (@Model.Button == ButtonRowModel.ButtonType.Start)
{
    <div class="col-6 d-none d-lg-block">
        <button class="col-3" id="startBtnLG" href="@Model.TargetURL">Start now</button>
    </div>

    <button class="col-12 d-lg-none" id="startBtnSm" href="@Model.TargetURL">Start now</button>
}

@if (@Model.Button == ButtonRowModel.ButtonType.Next)
{
    <div class="col-6 d-none d-lg-block">
        <button class="col-3" id="previousBtnLG" href="@Model.PreviousURL">Previous</button>
        <button class="col-3" id="nextBtnLG" href="@Model.TargetURL">Next</button>
    </div>

    <div class="col-12 d-lg-none">
        <button class="col-6" id="previousBtnSM" href="@Model.PreviousURL">Previous</button>
        <button class="col-6" id="nextBtnSM" href="@Model.TargetURL">Next</button>
    </div>
}

@if (@Model.Button == ButtonRowModel.ButtonType.Submit)
{
    <div class="col-6 d-none d-lg-block">
        <button class="col-3" id="previousBtnLG" href="@Model.PreviousURL">Previous</button>
        <button class="col-3" id="submitBtnLG" href="@Model.TargetURL">Submit</button>
    </div>

    <div class="col-12 d-lg-none">
        <button class="col-6" id="previousBtnSM" href="@Model.PreviousURL">Previous</button>
        <button class="col-6" id="submitBtnSM" href="@Model.TargetURL">Submit</button>
    </div>
}

异常信息

空引用异常


核心问题与解决步骤

1. 移除局部视图的@page指令

局部视图是页面复用片段,不是独立Razor页面,@page会使其成为独立页面,导致模型绑定逻辑异常。修改后的局部视图头部:

@model MyPages.Pages.Shared.components.ButtonRowModel
@{}

2. 模型类不要继承PageModel

PageModel是给独立Razor页面设计的基类,局部视图只需普通ViewModel。修改模型代码:

namespace MyPages.Pages.Shared.components
{
    public class ButtonRowModel // 移除: PageModel
    {
        public enum ButtonType
        {
            Start,
            Next,
            Submit
        }

        public ButtonType Button { get; set; }
        public string TargetURL { get; set; }
        public string? PreviousURL { get; set; }
    }
}

3. 修复按钮跳转属性错误(功能优化)

HTML<button>标签没有href属性,要实现跳转需改为<a>标签或添加点击事件:

  • 改为链接样式按钮:
<a class="col-3 btn" id="startBtnLG" href="@Model.TargetURL">Start now</a>
  • 保留按钮并添加跳转逻辑:
<button class="col-3 btn" id="startBtnLG" onclick="window.location.href='@Model.TargetURL'">Start now</button>

4. 明确初始化所有必要参数

渲染Next/Submit类型按钮时,需确保PreviousURL被赋值,避免空值导致的跳转失效或潜在异常:

@Html.Partial("Shared/components/ButtonRow", new ButtonRowModel()
{
    Button = ButtonRowModel.ButtonType.Next,
    TargetURL = "/NextPage",
    PreviousURL = "/PreviousPage"
})

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 11:35:27