Is it possible to check TempData inside if condtion in Asp.Net MVC?

1.1k views Asked by At

I want to check TempData inside of if condition. But I am getting an error.

My Controller

public ActionResult Customer(PurchaseViewModel purchaseviewmodel)
{
    TempData["Fromdt"] = purchaseviewmodel.FromDate;
    TempData["todt"] = purchaseviewmodel.ToDate;
    If(TempData["Fromdt"] == Convert.ToDateTime(“01/01/0001”)&& TempData["todt"] == Convert.ToDateTime(“01/01/0001”))
    {
        //...
    }
    else
    {
        //...
    }
    return View(Customer);
}

Why I am getting model values in Tempdata means I want to pass the values which I am getting in TempDate to another action . So only I am using TempData. Now I am getting error. The Error is

Operator == is not applied between object and System.DateTime.

I tried my level best to explain the issue. So any one help me to resolve this issue. And I need TempData only not to store values directly in variable. I can able to store the value in variable like

    var  fmdt = purchaseviewmodel.FromDate;
    var  todt = purchaseviewmodel. ToDate;

But my requirement to store values in TempData only that is my requirement because I need to use that TempData values in another action. I need for another purpose

1

There are 1 answers

0
Nkosi On BEST ANSWER

Temp data stores and exposes an object so == wont work when trying to compare to DateTime in your case.

You need to cast the object exposed by TempData to do your comparison.

Also no need to convert the string to datetime. You can use DateTime.MinValue

if((Datetime)TempData["FromDate"] == DateTime.MinValue)