Pass object to second page display null although it assigned correctly with data ?

Ahmed Salah Abed Elaziz 390 Reputation points
2023-04-08T20:55:11.4666667+00:00

I worked on blazor server side . I face issue when try to pass object UserdDto from page to page using service . my question why ApplicationStates.UserDto display null on second page although it correctly assigned on first page . what I try as below 1-create ApplicationStates service as below

public class ApplicationStates
    {
        public UserDto UserDto { get; set; }
    }
public class UserDto
    {

        [Required]
        public string UserName { get; set; }

        [Required]
        public string Password { get; set; }
        public bool IsActive { get; set; }
    }

2-on login component page (first page) when success login I assign values of user name and password login to ApplicationStates.UserDto to values username :AElaziz and password:ucs@12345

[Inject]
        public ApplicationStates ApplicationStates { get; set; }
 if (responseContent.ToString() == "Succed")
                        {

                            ApplicationStates.UserDto = userDto;
                          from debug username :AElaziz and password:ucs@12345

3- on second page Dashboard On Initialized event ApplicationStates.UserDto display null

@inject ApplicationStates ApplicationStates
 public UserDto useres;
    
    protected override async Task OnInitializedAsync()
    {
         useres= ApplicationStates.UserDto;
    }

4- on startup.cs I add on configure service services.AddScoped<ApplicationStates>(); when check ApplicationStates.UserDto it display null on dashboard Page(second page) so why ApplicationStates.UserDto display null on second page (dashboard) although it initialized correctly on first page(login) How to solve this issue Expected result second page ApplicationStates.UserDto must have data username :AElaziz and password:ucs@12345 assigned on first page login . Issues details Null initializedasync

Developer technologies ASP.NET ASP.NET Core
Developer technologies .NET Blazor
{count} votes

1 answer

Sort by: Most helpful
  1. Qing Guo - MSFT 896 Reputation points Microsoft External Staff
    2023-04-10T05:53:43.5966667+00:00

    Hi @Ahmed Salah Abed Elaziz ,

    so why ApplicationStates.UserDto display null on second page (dashboard) although it initialized correctly on first page(login)

    Because you on startup.cs add on configure service :

    services.AddScoped<ApplicationStates>();
    

    Scoped — Services are created on each request (once per request). So for example, if during a request you use the same dependency injection, in many places, you will use the same instance of that object, it will make reference to the same memory allocation. But when you go to the second page (dashboard), it's the another request, so the Services are created again, and you don't give the value to ApplicationStates.UserDto , that's why ApplicationStates.UserDto it display null on dashboard Page(second page).

    How to solve this issue Expected result second page ApplicationStates.UserDto must have data

    Singleton — Services are created once for the lifetime of the application. It uses the same instance for the whole application. So try to on startup.cs add on configure service:

       services.AddSingleton<ApplicationStates>();
    

    result: Capture


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Best regards,
    Qing Guo

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.