The code snippet below might indicate that you do not understand how TempData and the Keep() method work in ASP.NET Core as the code is redundant.
TempData["pid"] = 1003;
TempData.Keep("pid")
TempData is kept for one read. Once read, TempData is marked for deletion and not available on subsequent requests. Use Peek(string) to fetch an indexed item from TempData without marking the item for deletion.
I assume you read the "pid" value somewhere along the way when you should have used Peek().
var temp = TempData.Peek("pid");
Another way to do the same is...
int id = Convert.ToInt32(TempData["pid"]);
TempData.Keep("pid");
it get the value form DB and i try follow your instructions
but from the first time i try used temp data return 0
TempData has been around a long time and we know TempData works as expected. Most likely you have other issue with your code. Maybe the query result set is empty. You'll need to debug your code.
IMHO, this a very poor design. I recommend using a standard route parameter. The Id is cached in the URL. There is little reason copy the id to another cache.
If this is a Web API application then you should not use TempData because clients that consume your application will not know they must fetch a cookie from the HTTP response.
Now that you have posted more code, I agree with
mgebhard. This design makes no sense. There is no need to cache the ID and get it later. Just pass the id into NewTreatment following convention.
i understand that but as i know asp-route is working between two page in my situation user will open the patient page where the patient id that i want
and then when user click open bootstrap model that contain form and submit that where i want to use it
Your reasoning for the TempData design does not make sense. A modal is part of the View (DOM) that runs in the browser. The standard approach is passing the id to the action. This can be done with a route parameter or a form post.
Share all the relevant code if you need a community code review or debug support.
Member
1 Points
48 Posts
TempData
Nov 20, 2019 06:29 PM|ohoud|LINK
hi
i am facing problem
in one action i store value in temp data
i call it in anther action
and every thing work correctly in VS but when i deploy it it is not work
and i found it always return 0
Participant
1310 Points
442 Posts
Re: TempData
Nov 20, 2019 06:48 PM|deepalgorithm|LINK
The default storage mechanism for TempData is cookies. So I'm guessing its an issue with whether or not you configured cookie policy correctly.
Cookies and Consent in ASP .NET Core
Member
1 Points
48 Posts
Re: TempData
Nov 20, 2019 07:06 PM|ohoud|LINK
it compare mine with your link and everything is similar
but for me in deploying it is not working
All-Star
52201 Points
23274 Posts
Re: TempData
Nov 20, 2019 07:38 PM|mgebhard|LINK
The code snippet below might indicate that you do not understand how TempData and the Keep() method work in ASP.NET Core as the code is redundant.
TempData is kept for one read. Once read, TempData is marked for deletion and not available on subsequent requests. Use Peek(string) to fetch an indexed item from TempData without marking the item for deletion.
I assume you read the "pid" value somewhere along the way when you should have used Peek().
Another way to do the same is...
See the following.
https://www.learnrazorpages.com/razor-pages/tempdata
Participant
1310 Points
442 Posts
Re: TempData
Nov 20, 2019 07:44 PM|deepalgorithm|LINK
Is your middleware registered in the correct order? In Startup.Configure()
app.UseCookiePolicy();
should be afterapp.UseMVC()
Please post your entire startup.cs code.
Member
1 Points
48 Posts
Re: TempData
Nov 20, 2019 08:09 PM|ohoud|LINK
actually i don't have specific value to temp data
it get the value form DB and i try follow your instructions
but from the first time i try used temp data return 0
All-Star
52201 Points
23274 Posts
Re: TempData
Nov 20, 2019 08:19 PM|mgebhard|LINK
TempData has been around a long time and we know TempData works as expected. Most likely you have other issue with your code. Maybe the query result set is empty. You'll need to debug your code.
Member
1 Points
48 Posts
Re: TempData
Nov 20, 2019 08:35 PM|ohoud|LINK
this is my code for declar the temp data
here when i call it
it work fin and correctly when i debug but when i deploy it it return 0
All-Star
52201 Points
23274 Posts
Re: TempData
Nov 20, 2019 09:04 PM|mgebhard|LINK
IMHO, this a very poor design. I recommend using a standard route parameter. The Id is cached in the URL. There is little reason copy the id to another cache.
If this is a Web API application then you should not use TempData because clients that consume your application will not know they must fetch a cookie from the HTTP response.
Participant
1310 Points
442 Posts
Re: TempData
Nov 20, 2019 11:28 PM|deepalgorithm|LINK
Now that you have posted more code, I agree with mgebhard. This design makes no sense. There is no need to cache the ID and get it later. Just pass the id into NewTreatment following convention.
Member
1 Points
48 Posts
Re: TempData
Nov 21, 2019 06:07 AM|ohoud|LINK
i understand that but as i know asp-route is working between two page in my situation user will open the patient page where the patient id that i want
and then when user click open bootstrap model that contain form and submit that where i want to use it
All-Star
52201 Points
23274 Posts
Re: TempData
Nov 21, 2019 10:53 AM|mgebhard|LINK
Your reasoning for the TempData design does not make sense. A modal is part of the View (DOM) that runs in the browser. The standard approach is passing the id to the action. This can be done with a route parameter or a form post.
Share all the relevant code if you need a community code review or debug support.
Member
1 Points
48 Posts
Re: TempData
Nov 27, 2019 10:17 AM|ohoud|LINK
as you say everything work fine after using asp-route