Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在mvc中,只有赋值、调用、递增、递减和新对象表达式可以用作语句_C#_Asp.net Mvc_Compiler Errors_Controller - Fatal编程技术网

C# 在mvc中,只有赋值、调用、递增、递减和新对象表达式可以用作语句

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

我试图通过在控制器中使用内联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") : 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实现有一个特定的标记。