POST模型到控制器方法

时间:2012-03-18 02:58:06

标签: asp.net-mvc asp.net-mvc-3 model-view-controller

在过去的几周里,我一直在快速学习MVC 3,但有些事情发生了,我几乎无法解决搜索时间问题。我正在开发一个简单的购物车,我正试图通过结账流程以线性路径传递数据。无论我尝试什么,我都无法将模型发布到下一个视图。

首先,使用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看起来像这样(抱歉,没有剃刀):

<%@ 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 &amp; 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

2 个答案:

答案 0 :(得分:3)

很可能您的模型状态无效。 Add this extension method并在行动的第一行调用它,如:

ModelState.DumpErrors();

在断言点后面放一行,检查“输出”窗口,了解有关绑定错误的更多信息。

编辑 - 完整的扩展方法:

public static class ModelExtensions
{
    public static void DumpErrors(this System.Web.Mvc.ModelStateDictionary ModelState)
    {
        var errors = from key in ModelState
                     let errorList = ModelState[key.Key].Errors
                     where errorList.Any()
                     select new
                     {
                         Item = key.Key,
                         Value = key.Value,
                         errorList
                     };

        foreach (var errorList in errors)
        {
            System.Diagnostics.Debug.WriteLine("MODEL ERROR:");
            System.Diagnostics.Debug.WriteLine(errorList.Item);
            System.Diagnostics.Debug.WriteLine(errorList.Value);
            foreach (var error in errorList.errorList)
            {
                System.Diagnostics.Debug.WriteLine(error.ErrorMessage);
                System.Diagnostics.Debug.WriteLine(error.Exception);
            }
            System.Diagnostics.Debug.WriteLine("-----");
        }
    }
}

答案 1 :(得分:0)

我不知道asp具体,但似乎你可以访问索引中的购物车并确认b / c你明确地传递它。由于您没有将其传递给结帐,您将无法访问它。我可能完全离开了