Home > Software design >  id parameter in OnGet hadler is reseting to 0 eveytime i refresh the page
id parameter in OnGet hadler is reseting to 0 eveytime i refresh the page

Time:04-26

I need to implement some functions in a razor page and i need "id" to have immutable value. I am passing "id" from other page into OnGet-handler on my main page. Then when I am refreshing the page,"id" is 0. I was trying different ways to keep the original value setted to "id". Is there any way to make my "id" immutable even if i refresh page many times?

 [BindProperty(SupportsGet = true)] public int InterestId { get; set; }

 public bool isSet { get; set; } 

    public async Task<IActionResult>  OnGet(int id)
    {

        if(id != null)  //Just in order to load all data, otherwise it jumps to view too fast 
        {

            //var interests = await apiManager.GetInterests();
            //Interest = interests.FirstOrDefault(x => x.Id == id);

            //var threads = await apiManager.ReturnAllThreads();
            //Threads = threads.Where(t => t.InterestId == id).ToList();

            //await searchBar.Search(SearchKey, id,  Threads);

            if (isSet == false)
            {
                isSet = true;   // It's false again after refreshing
                InterestId = id;
            }

        }
        return Page();

    }

CodePudding user response:

You can try to use Session to keep your id:

Startup.cs:

 public void ConfigureServices(IServiceCollection services)
            {
                ............
                services.AddDistributedMemoryCache();

                services.AddSession(options =>
                {
                    options.IdleTimeout = TimeSpan.FromDays(1);
                    options.Cookie.HttpOnly = true;
                    options.Cookie.IsEssential = true;
                });
            }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            .........

            app.UseAuthorization();

            //add UseSession here
            app.UseSession();

            app.UseEndpoints(endpoints =>
            {
                .........
            });
        }

cshtml.cs:

public async Task<IActionResult>  OnGet(int id)
    {

        if(id != null)  //Just in order to load all data, otherwise it jumps to view too fast 
        {

            //var interests = await apiManager.GetInterests();
            //Interest = interests.FirstOrDefault(x => x.Id == id);

            //var threads = await apiManager.ReturnAllThreads();
            //Threads = threads.Where(t => t.InterestId == id).ToList();

            //await searchBar.Search(SearchKey, id,  Threads);

            if (isSet == false)
            {
                isSet = true;   // It's false again after refreshing
                InterestId = id;
            }

        }
        return Page();

    }

CodePudding user response:

Thank you for replying. I walked around my problem just by adding additional if-statement, specifying what should be returned if id is equal to 0. Its not exactly the solution I was looking for but in terms of my project its good enough.

  • Related