locked
TempData RRS feed

  • Question

  • User-297906461 posted

    hi 

    i am facing problem 

    in one action i store value in temp data 

    TempData["pid"] = 1003;
    TempData.Keep("pid");

    i call it in anther action 

    int id=Convert.ToInt32(TempData["pid"]);

    and every thing work correctly in VS but when i deploy it it is not work 

    and i found it always return 0

    Wednesday, November 20, 2019 6:29 PM

Answers

  • User-297906461 posted

    as you say everything work fine after using asp-route

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 27, 2019 10:17 AM

All replies

  • User765422875 posted

    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

    Wednesday, November 20, 2019 6:48 PM
  • User-297906461 posted

    it compare mine with your link and everything is similar 

    but for me in deploying it is not working 

    Wednesday, November 20, 2019 7:06 PM
  • User475983607 posted

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

    See the following.

    https://www.learnrazorpages.com/razor-pages/tempdata

    Wednesday, November 20, 2019 7:38 PM
  • User765422875 posted

    Is your middleware registered in the correct order? In Startup.Configure() app.UseCookiePolicy(); should be after app.UseMVC()

    Please post your entire startup.cs code.

    Wednesday, November 20, 2019 7:44 PM
  • User-297906461 posted

    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

    Wednesday, November 20, 2019 8:09 PM
  • User475983607 posted

    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

    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. 

    Wednesday, November 20, 2019 8:19 PM
  • User-297906461 posted

    this is my code for declar the temp data

            public IActionResult PatientInformation(int id)
            {
                TempData["Patientid"] = id;
    ...
    }

    here when i call it 

     public IActionResult NewTretment(FreTretment Tretment)
            {
                var Id = TempData.Peek("Patientid");
                Tretment.PatientId = Convert.ToInt32(Id);
    ....
    }

    it work fin and correctly when i debug but when i deploy it it return 0

    Wednesday, November 20, 2019 8:35 PM
  • User475983607 posted

    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. 

    Wednesday, November 20, 2019 9:04 PM
  • User765422875 posted

    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.

    Wednesday, November 20, 2019 11:28 PM
  • User-297906461 posted

    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 

    Thursday, November 21, 2019 6:07 AM
  • User475983607 posted

    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.

    Thursday, November 21, 2019 10:53 AM
  • User-297906461 posted

    as you say everything work fine after using asp-route

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 27, 2019 10:17 AM