我有这个控制器:
[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");
}
}
}
我在AddressAndPayment
下创建了一个名为Checkout
的视图,因此转到localhost/Checkout/AddressAndPayment
,但我只收到404错误,即使我右键单击视图并点击在页面检查器中查看。我不知道为什么它在创建时甚至没有显示视图。
答案 0 :(得分:3)
您需要相应的HttpGet
方法,因为您当前的方法只接受HttpPost
请求。添加以下内容:
[HttpGet]
public ActionResult AddressAndPayment()
{
return View();
}