.netasp.net-corerazor-pagesviewstate

How to use ViewState logic in razor pages asp.net core application?


I am having trouble converting the logic of the below lines in my code while migrating an ASP.NET web form application to an ASP.NET core razor pages application. Can you guide me on how to use ViewState in razor pages for this?

ViewState["Client"] = client;

I attempted to research state management in .NET Core, but I was unable to determine which logic could substitute for the ViewState ASP.NET control.


Solution

  • In ASP.NET Core, there isn't a direct equivalent of the ViewState concept from ASP.NET Web Forms. But here are several methods you can refer to, Hope it can give you some help:

    [BindProperty] attribute: In Razor Pages, you can use model binding to bind data directly from the HTTP request to your page model.

    [BindProperty]
    public string Client { get; set; }
    

    After add this property to your property, This property will implement two-way binding with the front-end input tag.

    [TempData] This property stores data until it's read in another request.

    Or as Mike Brind said, a hidden form field is also a solution.