创建视图上的 MVC 4 错误 404
本文关键字:错误 MVC 视图 创建 | 更新日期: 2023-09-27 18:33:27
我有这个控制器:
[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");
}
}
}
而且我在Checkout
下创建了一个名为AddressAndPayment
的视图,所以它转到localhost/Checkout/AddressAndPayment
,但我只收到404错误,即使我右键单击视图并单击页面检查器中的视图。我不知道为什么它在创建时甚至不显示视图。
您需要相应的HttpGet
方法,因为您当前的方法只接受HttpPost
请求。添加以下内容:
[HttpGet]
public ActionResult AddressAndPayment()
{
return View();
}