【问题标题】:Only assignment, call, increment, decrement, and new object expressions can be used as a statement in mvcmvc中只有赋值、调用、自增、自减、new对象表达式可以作为语句使用
【发布时间】:2017-05-12 09:35:51
【问题描述】:

我正在尝试通过在我的控制器中使用 inline 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");
    }

我在最后一行得到这个错误

只有赋值、调用、递增、递减和新对象表达式可以用作语句

我们如何解决这个问题?

【问题讨论】:

  • 请注意,model-view-controller 标签是针对有关模式的问题。 ASP.NET-MVC 实现有一个特定的标签。

标签: c# asp.net-mvc compiler-errors controller


【解决方案1】:

你应该添加一个return语句:

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;

延伸阅读:Statements (C# Programming Guide)

【讨论】:

  • 我不敢相信这是问题所在,提前 tnx
猜你喜欢
  • 1970-01-01
  • 2018-11-13
  • 2019-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-27
  • 1970-01-01
相关资源
最近更新 更多