1

我有这个控制器:

[Authorize]
public class CheckoutController : Controller
{

    ShoppingCartContext storeDB = new ShoppingCartContext();
    const string PromoCode = "FREE";

    [HttpPost]
    public ActionResult AddressAndPayment(FormCollection values)
    {
        var order = new Order();
        TryUpdateModel(order);

        try
        {
            if (string.Equals(values["PromoCode"], PromoCode,
                StringComparison.OrdinalIgnoreCase) == false)
            {
                return View(order);
            }
            else
            {
                order.Username = User.Identity.Name;
                order.OrderDate = DateTime.Now;

                //Save Order
                storeDB.Orders.Add(order);
                storeDB.SaveChanges();
                //Process the order
                var cart = Models.ShoppingCart.GetCart(this.HttpContext);
                cart.CreateOrder(order);

                return RedirectToAction("Complete",
                    new { id = order.OrderId });
            }
        }
        catch
        {
            //Invalid - redisplay with errors
            return View(order);
        }
    }

    public ActionResult Complete(int id)
    {
        // Validate customer owns this order
        bool isValid = storeDB.Orders.Any(
            o => o.OrderId == id &&
            o.Username == User.Identity.Name);

        if (isValid)
        {
            return View(id);
        }
        else
        {
            return View("Error");
        }
    }

}

而且我创建了一个名为AddressAndPaymentunder的视图Checkout,所以它会转到localhost/Checkout/AddressAndPayment但我只会收到 404 错误,即使我右键单击视图并单击页面检查器中的视图。我不知道为什么它在创建时甚至不显示视图。

4

1 回答 1

4

您需要一个相应的HttpGet方法,因为您当前的方法只接受一个HttpPost请求。添加以下内容:

[HttpGet]
public ActionResult AddressAndPayment()
{
    return View();
}
于 2013-10-01T21:36:15.543 回答