RazorPage 绑定未按预期工作

RazorPage binding not working as expected

我有以下 HTML:

<label asp-for="CurrentLocation.Description" class="text-info"></label>

并且在绑定模型中:

public class NavigationModel : PageModel
{
    public void OnGet()
    {
        CurrentLocation = new Location()
        {
            Description = "test"
        };
    }

    [BindProperty]
    public Location CurrentLocation { get; set; }
}

因此,根据我对 RazorPages 的理解,站点应该显示 "test";但相反,它显示 "Description"。我看过几个例子,在这些例子中,这种事情运行良好,但我不明白为什么我的版本可能会有所不同。谁能指出我正确的方向?

根据documentation,您观察到的是正确的值:Description

The asp-for attribute is made available by the For property in the LabelTagHelper. See Authoring Tag Helpers for more information.

用 Razor 的说法,标签从未显示 属性 的值,而是显示它的名称。

要显示值,请使用

<label asp-for="CurrentLocation.Description" for="theValue" class="text-info">
</label>
<span id="theValue"> @model.CurrentLocation.Description</span>

label 标签将只显示您使用的 属性 的名称。

如果您想显示包含说明内容的输入 属性,则必须输入以下代码:

<input asp-for="CurrentLocation.Description" type="hidden" />

如果您想将 属性 的值显示为标签,则必须键入以下内容:

@CurrentLocation.Description