Asp.net 局部视图:未定义模型
TLDR:问题是,当我尝试从模型(下面代码的第8行)获取ID时,我看到了以下错误: System.NullReferenceException:对象引用未设置为对象的实例 详情: 我想让HTML的一部分可重用。有一个名为“InfoBox”的大div,它包含一些信息,放在我网站的多个位置。我决定创建部分剃须刀页面。我是这样做的:Asp.net 局部视图:未定义模型,asp.net,asp.net-core,razor-pages,Asp.net,Asp.net Core,Razor Pages,TLDR:问题是,当我尝试从模型(下面代码的第8行)获取ID时,我看到了以下错误: System.NullReferenceException:对象引用未设置为对象的实例 详情: 我想让HTML的一部分可重用。有一个名为“InfoBox”的大div,它包含一些信息,放在我网站的多个位置。我决定创建部分剃须刀页面。我是这样做的: \u TextBoxInfoPartial.cshtml.cs: public class _TextBoxInfoPartialModel : PageModel {
\u TextBoxInfoPartial.cshtml.cs
:
public class _TextBoxInfoPartialModel : PageModel
{
[BindProperty]
public string ID { get; set; }
public IActionResult OnGet()
{
ID = ""; //default
return Page();
}
}
\u TextBoxInfoPartial.cshtml
:
@page
@model Definicje.Pages.Shared._TextBoxInfoPartialModel
<button type="button" class="visibility-toggler" visibility-toggler="#hint-math@(Model.ID)">Wstaw wzór</button>
<div id="hint-math@(Model.ID)" class="hintbox">
some text
</div>
我不明白。我正在为Partial指定模型,为什么它没有定义?对于Partial视图,请删除
@page
指令
@model Definicje.Pages.Shared._TextBoxInfoPartialModel
<button type="button" class="visibility-toggler"
visibility-toggler="#hint-math@(Model.ID)">Wstaw wzór</button>
<div id="hint-math@(Model.ID)" class="hintbox">
some text
</div>
@model Definicje.Pages.Shared.\u TextBoxInfoPartialModel
Wsaw wzór
一些文本
现在,Model
将是您从另一个视图传递的\u TextBoxInfoPartialModel
对象
当您将@page
指令添加到视图文件时,它开始表现出不同的行为,更像是一种操作方法—您可以通过http请求访问它
另一个观察结果是,如果文件名以前缀开头,则上述路由功能将不起作用。Http请求无法直接访问它
因此,对于部分视图,不要使用@page
指令
@model Definicje.Pages.Shared._TextBoxInfoPartialModel
<button type="button" class="visibility-toggler"
visibility-toggler="#hint-math@(Model.ID)">Wstaw wzór</button>
<div id="hint-math@(Model.ID)" class="hintbox">
some text
</div>