[英]Is it possible to check TempData inside if condtion in Asp.Net MVC?
I want to check TempData
inside of if
condition. 我想检查if
条件内的TempData
。 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 . 为什么在Tempdata中获取模型值意味着我想将在TempDate中获取的值传递给另一个动作。 So only I am using TempData. 所以只有我在使用TempData。 Now I am getting error. 现在我出错了。 The Error is 错误是
Operator == is not applied between object and System.DateTime. 运算符==不适用于对象和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. 而且我只需要TempData而不是将值直接存储在变量中。 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. 但是我只需要在TempData中存储值,这是我的要求,因为我需要在另一个操作中使用该TempData值。 I need for another purpose 我需要另一个目的
Temp data stores and exposes an object
so ==
wont work when trying to compare to DateTime
in your case. 临时数据存储并公开一个object
因此在尝试与您的案例中的DateTime
进行比较时, ==
无法工作。
You need to cast the object exposed by TempData to do your comparison. 您需要转换TempData公开的对象以进行比较。
Also no need to convert the string to datetime. 也无需将字符串转换为datetime。 You can use DateTime.MinValue
您可以使用DateTime.MinValue
if((Datetime)TempData["FromDate"] == DateTime.MinValue)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.