简体   繁体   中英

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM