Home > front end >  Razor Pages ModelState is different (invalid) when "nullable" types are enabled. WHY?
Razor Pages ModelState is different (invalid) when "nullable" types are enabled. WHY?

Time:12-30

I have tried to copy/rewrite that code for learning purposed and realized after far to much hours:

sneakly hidden before all the using statements: #nullable disable

It was driving me insane, that in my code (opposed to example) that the ModelState was always invalid with the parameter returnUrl present in the method signature:

public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
if (ModelState.IsValid)
  {
  //...
  }
}

WHY ?

I was under the impression that, enabling nullable types, WILL NEVER break backwards-compatibility as in simply enabling/disabling it would never change how the same code works. But appareantly, the framework does some black magic and reports a different value for ModelState. This is extremely unexpected (for me). That means that actually a bool holds different value, simply by enabling/disabling nullability!

Sidenote: Since I try to work through the example, I really don't need the paramter for my implementation, and I assume, maybe, when using parameters (I assume from queries) like this, the implementaion could be made more stable, that either nulls are never generated or nulls are accaptable for the action handler ??

But I'm scared now, do I have to pay special attention to the code in various places, where the RazorPages/ASP.NET Core/Blazor framework behaves differently for nullability enabled/disabled?

What are best practices? Simply disable nullable for RazorPages like in example?

Critique about 'nullable' in Razor Pages:

https://andrewlock.net/fighting-with-nullable-reference-types-in-razor-pages/

CodePudding user response:

Prior to .NET 6, new projects do not include the Nullable element. Beginning with .NET 6, new projects include the <Nullable>enable</Nullable> element in the project file.

The returnUrl property is non-nullable(Equivalent to hermit adding a [Required] property) by default, So if you don't bind any value to this property, ModelState.IsValid will return false.

If you don't want this, you can disable this by deleting the below line from the csproj file or setting it as disable. By default(.Net 6) value is enable.

<Nullable>enable</Nullable>

Or, you can just add ? to make it nullable:

(string? returnUrl = null)

Or, make ModelState ignore this property:

if (ModelState.Remove("returnUrl"))
{
      *save form, go somewhere*
}
  • Related