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
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.
By default TempData uses the ASP.NET Session as storage. So it is stored on the server ( InProc is the default).
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With