【问题标题】:POST Model to Controller MethodPOST 模型到控制器方法
【发布时间】: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 &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

【问题讨论】:

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


    【解决方案1】:

    您的模型状态很可能无效。 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("-----");
            }
        }
    }
    

    【讨论】:

    • 您确实教会了我一个查找模型错误的好方法,它确实让我朝着正确的方向前进。这是我试图用作模型的“Entities.OrderDetail”的问题。该实体只拥有 2 个公共对象,它们都是 Linq to SQL 对象。也许我不能用它作为模型,必须使用 POCO 对象?
    【解决方案2】:

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

    【讨论】:

    • 问题不在于访问购物车对象,由于我的 IModelBinder 实现,我可以随时使用它。我得到一个空的 OrderDetail。我认为 asawyer 已经为我指明了正确的方向……
    猜你喜欢
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 2011-10-22
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    相关资源
    最近更新 更多