gpt4 book ai didi

c# - mvc中只能使用assignment、call、increment、decrement、new对象表达式作为语句

转载 作者:行者123 更新时间:2023-11-30 23:12:44 26 4
gpt4 key购买 nike

我正在尝试通过在我的 Controller 中使用内联来从我在数据库中的一个表中获取金额

    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");
}

正如错误信息所说,三元运算符本身并不是一个语句。它可以是另一条语句的一部分——例如返回语句如上,或赋值语句

   int value = condition ? 0 : 42;

进一步阅读:Statements (C# Programming Guide)

关于c# - mvc中只能使用assignment、call、increment、decrement、new对象表达式作为语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43934299/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com