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")
{
}

Developer technologies | ASP.NET | Other
{count} votes

Answer accepted by question author
  1. Lan Huang-MSFT 30,206 Reputation points Microsoft External Staff
    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' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.