C# 在mvc中,只有赋值、调用、递增、递减和新对象表达式可以用作语句
我试图通过在控制器中使用内联if从DB中的一个表中获取数量C# 在mvc中,只有赋值、调用、递增、递减和新对象表达式可以用作语句,c#,asp.net-mvc,compiler-errors,controller,C#,Asp.net Mvc,Compiler Errors,Controller,我试图通过在控制器中使用内联if从DB中的一个表中获取数量 public ActionResult IfPaid(int id) { Ref_ViewModel = new View_model.View_Model(); Ref_ViewModel.GetAllCustomers(id).Any(p => p.Paid == false) ? RedirectToAction("Pay", "Account") : RedirectToAc
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");
}
我在最后一行得到这个错误
只能将赋值、调用、递增、递减和新对象表达式用作语句
如何解决此问题?您应该添加一条返回语句:
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");
}
正如错误消息所说,三元运算符本身不是一个语句。它可以是另一个语句的一部分,例如上面的返回语句或赋值语句
int value = condition ? 0 : 42;
进一步阅读:我不相信这会造成问题,请注意模型-视图-控制器标记用于解决有关模式的问题。ASP.NET-MVC实现有一个特定的标记。