© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
1 reply
Eistee

❔ Razor Pages Session for revalidating Page Model

Can someone help me to configure the Session for Razor Pages, that I can use
TryValidateModel()
TryValidateModel()
?

TryValidateModel(this);"
TryValidateModel(this);"

is throwing a
InvalidOperationException: Session has not been configured for this application or request.
InvalidOperationException: Session has not been configured for this application or request.
error?

I can't find any hint about the online
https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.razorpages.pagemodel.tryvalidatemodel?view=aspnetcore-7.0

The debugger is actually stepping over it, but the trace says:
Microsoft.AspNetCore.Http.DefaultHttpContext.get_Session()
Microsoft.Extensions.Internal.PropertyHelper.CallNullSafePropertyGetter<TDeclaringType, TValue>(Func<TDeclaringType, TValue> getter, object target)
Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationEntry.get_Model()
Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy strategy)
Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType(IValidationStrategy defaultStrategy)
Microsoft.AspNetCore.Http.DefaultHttpContext.get_Session()
Microsoft.Extensions.Internal.PropertyHelper.CallNullSafePropertyGetter<TDeclaringType, TValue>(Func<TDeclaringType, TValue> getter, object target)
Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationEntry.get_Model()
Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy strategy)
Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType(IValidationStrategy defaultStrategy)


The server side page Post method looks like this:
public async Task<IActionResult> OnPostAsync()
{
    if (!ModelState.IsValid)
    {
        for (int x = ThirdPartyInformations.Count -1; x >= 0; x--) 
        {
            if (ThirdPartyInformations[x].HiddenEntry == true)
            {
                ThirdPartyInformations.RemoveAt(x);
            }
        }

        ModelState.Clear();
        TryValidateModel(this); // <-- 
    }

    if (!ModelState.IsValid ||
    ....
public async Task<IActionResult> OnPostAsync()
{
    if (!ModelState.IsValid)
    {
        for (int x = ThirdPartyInformations.Count -1; x >= 0; x--) 
        {
            if (ThirdPartyInformations[x].HiddenEntry == true)
            {
                ThirdPartyInformations.RemoveAt(x);
            }
        }

        ModelState.Clear();
        TryValidateModel(this); // <-- 
    }

    if (!ModelState.IsValid ||
    ....
PageModel.TryValidateModel Method (Microsoft.AspNetCore.Mvc.RazorPa...
Validates the specified model instance.
PageModel.TryValidateModel Method (Microsoft.AspNetCore.Mvc.RazorPa...
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ What is the purpose of `ModelBindingContext.Model`, `ModelName`, and `ModelBinderAttribute.Name`?
C#CC# / help
3y ago
❔ Page debugging twice in razor pages PageRouteModelConvention
C#CC# / help
4y ago
❔ Razor pages redirect
C#CC# / help
4y ago
❔ Razor pages view
C#CC# / help
4y ago