简体   繁体   中英

id parameter in OnGet hadler is reseting to 0 eveytime i refresh the page

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();

    }

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();

    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM