Search code examples
c#razor-pagesasp.net-core-tag-helpers

RazorPage binding not working as expected


I have the following HTML:

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

And in the bound model:

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

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

So, to my understanding of RazorPages, the site should display "test"; but instead, it displays "Description". I've looked at several examples where this sort of thing works fine, and I can't see why my version might be different. Can anyone point me in the right direction?


Solution

  • According to the documentation, what you observe is the correct value: Description.

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

    Labels, in Razor parlance, have never displayed the value of a property, but rather the name if it.

    To display the value use something like

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