Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 790
susan Avatar asked Dec 31 '16 11:12

susan


People also ask

Can we access TempData in view?

TempData is a property in the ControllerBase class. So, it is available in any controller or view in the ASP.NET MVC application. The following example shows how to transfer data from one action method to another using TempData.

Where is TempData stored in MVC?

By default TempData uses the ASP.NET Session as storage. So it is stored on the server ( InProc is the default).

How does TempData work in MVC?

What is TempData and How to Use in MVC? TempData is used to transfer data from the view to the controller, the controller to the view, or from an action method to another action method of the same or a different controller. TempData temporarily saves data and deletes it automatically after a value is recovered.


1 Answers

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)
like image 165
Nkosi Avatar answered Sep 19 '22 22:09

Nkosi