代码之家  ›  专栏  ›  技术社区  ›  Paul Michaels

RazorPage绑定未按预期工作

  •  0
  • Paul Michaels  · 技术社区  · 8 年前

    我有以下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 回复  |  直到 8 年前
        1
  •  0
  •   Sam Axe    8 年前

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

    asp for属性由中的for属性提供 LabelTagHelper . 看见 Authoring Tag Helpers 了解更多信息。

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

    要显示值,请使用以下命令

    <label asp-for="CurrentLocation.Description" for="theValue" class="text-info">
    </label>
    <span id="theValue"> @model.CurrentLocation.Description</span>
    
        2
  •  0
  •   pitaridis    8 年前

    标签标记将只显示您使用的特性的名称。

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

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

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

    @CurrentLocation.Description
    
    推荐文章