RazorPage绑定未按预期工作

时间:2018-01-22 22:59:11

标签: c# razor-pages asp.net-core-tag-helpers

我有以下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”;但相反,它显示“描述”。我看过几个这样的事情很好的例子,我看不出为什么我的版本会有所不同。有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:0)

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

  

asp {for}属性由LabelTagHelper中的For属性提供。有关详细信息,请参阅Authoring Tag Helpers

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

要显示值,请使用

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

答案 1 :(得分:0)

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

如果要显示包含Description属性内容的输入,则必须输入以下代码:

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

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

@CurrentLocation.Description