I fill my TempData from a FormCollection
and then I try to check the value of my TempData
in my view with MVC 4 but my if
statement is
Before starting any block of code in MVC View always start using @{ } then write any line of code and terminate with semicolon(;)
Please try this
var tempval = TempData["var"];
then write your if statement as follow
@if (tempval.ToString() == "abcd")
{
<span>Check</span> //Never displayed
}
else
{
<span>@tempval</span>; // Display "abcd"
}
Try change TempData.Add("var", "abcd");
to
TempData['var'] = "abcd";
Update:
In My controller:
public ActionResult Index()
{
TempData["var"] = "abcd";
return View();
}
In my view:
// I cast to string to make sure it's checking for the correct TempData (string)
@if ((string)TempData["var"] == "abcd")
{
<span>Check</span>
}
else
{
@TempData["var"].ToString()
}