并非所有代码路径都会为计时器返回值错误

时间:2017-05-26 11:13:03

标签: c# timer asp.net-mvc-5

订单控制器

public async Task<ActionResult> FirstClassCreate(FormCollection values)
{

    var order = new Order();
    TryUpdateModel(order);
    var customer = db.Users.FirstOrDefault(x => x.Email == User.Identity.Name);
    var cart = ShoppingCart.GetCart(this.HttpContext);

    try
    {
        //I need to get the order.timeleft to start a count down

        order.timeleft = timer(); 
        //Save Order
        storeDB.Orders.Add(order);
        await storeDB.SaveChangesAsync();
        //Process the order
        cart = ShoppingCart.GetCart(this.HttpContext);
        order.Total = cart.GetFirstClass();

        order = cart.CreateOrder(order);

        return RedirectToAction("FirstClass", "Checkouts");
    }
    catch
    {
        //Invalid - redisplay with errors
        return View(order);
    }
}

我需要计时器每秒减1,一个时间= 0返回timerleft为false,但是我遇到了公共ActionResult计时器突出显示定时器状态&#39; .timer(Order)&#39的问题;:并非所有代码路径都返回值

public ActionResult timer(Order order)
{
    int time;
    order.timeleft = true;
    time = 30;
    do
    {
        if (time > 0)
        {


            time = time - 1;
        }
        else
        {
            // If the user ran out of time, stop the timer, set timeleft = false
            var orderToBeChanged = db.Orders
        .Where(o => o.HasPaid == false && o.Email == User.Identity.Name)
              .OrderByDescending((o => o.OrderDate)).First();
            order.timeleft = false;
            db.SaveChanges();


        }
    } while (time > 0);
}

2 个答案:

答案 0 :(得分:0)

在任何地方的函数public ActionResult timer(Order order)中你都没有返回任何值,而在标题中你说这个函数返回类型ActionResult

在您的情况下,您可以通过以下方式修改timer功能:

public bool timer(Order order)
{
    ...
    } while (time > 0);

    return order.timeleft;

}

答案 1 :(得分:0)

ActionResult类型的方法必须返回一个ActionResult,即

public ActionResult Timer(Order order)
{
    ...
    return myActionResult;
}

或者,如果您不希望它返回任何标记为无效

public void Timer(Order order)
{
    ...
}