asp.net-corerazor-pagesmodel-bindingasp.net-core-7.0c#-record-type

ASP.NET Core's BindNever does not work with record


When using Razor Pages 7 I have this in the code-behind file:

public record PersonDto(
  long Id,
  string Name,
  [BindNever] string Address,
  // ...
);

[BindProperty] public PersonDto Person { get; set; }

I populate that in the OnGet() and show it on the page. I read it in the OnPost() to update the database.

But Address is supposed to be read-only. I do not allow it to be changed, so the post handler always fails with a validation error of "The Address field is required".

The BindNever attribute does not work on the record (even though the docs show that it should).

I also tried the long-form record syntax, and the [property:BindNever] syntax, with the same result.

Anyone know why?


Solution

  • I found a workaround. I replaced this:

    [BindNever] string Address,
    

    with this:

    [property:BindNever] string? Address,