我正在尝试通过在 Controller 中使用内联 if 来从数据库中的一个表中获取金额
public ActionResult IfPaid(int id)
{
Ref_ViewModel = new View_model.View_Model();
Ref_ViewModel.GetAllCustomers(id).Any(p => p.Paid == false) ? RedirectToAction("Pay", "Account") : RedirectToAction("Download");
}
我在最后一行收到此错误
Only assignment, call, increment, decrement, and new object expressions can be used as a statement
我们如何解决这个问题?
最佳答案
您应该添加一个返回语句:
public ActionResult IfPaid(int id)
{
Ref_ViewModel = new View_model.View_Model();
return Ref_ViewModel.GetAllCustomers(id).Any(p => p.Paid == false)
? RedirectToAction("Pay", "Account")
: RedirectToAction("Download");
}
正如错误消息所示,三元运算符本身并不是一个语句。它可以是另一个语句的一部分 - 例如上面的 return 语句,或者赋值语句
int value = condition ? 0 : 42;
关于c# - mvc中只能使用赋值、调用、自增、自减、new对象表达式作为语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43934299/