tempdata value in view not passing the condition

shijo stephen 21 Reputation points
2022-12-02T07:57:39.307+00:00

temp data value in view is not passing the condition

@{
var s = @TempData.Peek("DocumentStatus");
}

@if (s == "Application")
{
}

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,539 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 29,916 Reputation points Microsoft Vendor
    2022-12-02T08:56:43.383+00:00

    Hi @shijo stephen ,
    First you need to make sure there is a value,
    then you need to change var s = @TempData.Peek("DocumentStatus"); to string s = TempData.Peek("DocumentStatus").ToString();
    I did a simple test, you can refer to it.

     public ActionResult Index()  
            {  
                TempData["DocumentStatus"] = "Application";  
                return View();  
            }  
    

    @{  
      
        string s = TempData.Peek("DocumentStatus").ToString();  
    }  
      
    @if (s == "Application")  
    {  
        string a = "";  
    }  
    

    266499-image.png
    Best regards,
    Lan Huang


    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

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.