【发布时间】:2012-03-18 02:58:05
【问题描述】:
在过去的几周里,我一直在快速学习 MVC 3,但出现了一些我无法解决的问题。我正在开发一个简单的购物车,并试图通过结帐过程以线性路径传递数据。无论我尝试什么,我都无法让模型 POST 到下一个视图。
首先,使用 IModelBinder 的实现从 Session 中提取“Cart”实体。它基本上适用于任何方法。它已经工作了一段时间。我的问题是试图在 /cart/confirm 和 /cart/checkout 之间传递相同的模型。
有人能帮忙弄清楚为什么 /cart/checkout 的控制器中模型总是为空吗?
public class CartController : Controller
{
public ActionResult Index (Cart cart)
{
//Works fine, anonymous access to the cart
return View(cart);
}
[Authorize]
public ActionResult Confirm (Cart cart)
{
//Turn 'Cart' from session (IModelBinder) into a 'Entities.OrderDetail'
OrderDetail orderDetail = new OrderDetail();
orderDetail.SubTotal = cart.ComputeTotalValue();
...
...
return View(orderDetail);
}
[Authorize]
public ActionResult Checkout(OrderDetail model)
{
//PROBLEM: model is always null here.
}
}
/Views/Cart/Index.aspx 看起来像这样(抱歉,不是 Razor):
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site-1-Panel.Master" Inherits="System.Web.Mvc.ViewPage<My.Namespace.Entities.Cart>" %>
...
...
<% using(Html.BeginForm("confirm", "cart")) { %>
Not much to see here, just a table with the cart line items
<input type="submit" value="Check Out" />
<% } %>
我怀疑问题出在这里,但我已经尝试了 Html.BeginForm() 的所有变体,但我无法将模型传递给 /cart/checkout。无论如何,/Views/Cart/Confirm.aspx 看起来像这样:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site-1-Panel.Master" Inherits="System.Web.Mvc.ViewPage<My.Namespace.Entities.OrderDetail>" %>
...
...
<% using (Html.BeginForm("checkout", "cart", Model)) { %>
<%: Model.DBUserDetail.FName %>
<%: Model.DBUserDetail.LName %>
<%: Html.HiddenFor(m => m.DBOrder.ShippingMethod, new { @value = "UPS Ground" })%>
<%: Html.HiddenFor(m => m.DBOrder.ShippingAmount, new { @value = "29.60" })%>
...
...
<input type="submit" value="Confirm & Pay" />
<% } %>
最后 /Views/Cart/Checkout.aspx 看起来像这样:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site-1-Panel.Master" Inherits="System.Web.Mvc.ViewPage<My.Namespace.Entities.OrderDetail>" %>
...
...
<%: Html.Hidden("x_first_name", Model.DBUserDetail.FName) %>
<%: Html.Hidden("x_last_name", Model.DBUserDetail.LName) %>
...
It doesn't really matter what's here, an exception gets throw in the controller because the model is always null
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-3 model-view-controller