【问题标题】:RazorPage binding not working as expectedRazorPage 绑定未按预期工作
【发布时间】:2018-05-12 06:46:43
【问题描述】:

我有以下 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 的理解,该站点应该显示“测试”;但相反,它显示“描述”。我看过几个例子,这些例子都很好,但我不明白为什么我的版本可能会有所不同。谁能指出我正确的方向?

【问题讨论】:

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


    【解决方案1】:

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

    LabelTagHelper 中的 For 属性使 asp-for 属性可用。请参阅Authoring Tag Helpers 了解更多信息。

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

    要显示值,请使用

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

    【讨论】:

      【解决方案2】:

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

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

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

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

      @CurrentLocation.Description
      

      【讨论】:

        猜你喜欢
        • 2010-09-19
        • 2012-08-17
        • 2018-03-09
        • 2019-04-23
        • 1970-01-01
        • 1970-01-01
        • 2012-01-30
        • 2020-09-13
        • 2021-03-05
        相关资源
        最近更新 更多