【问题标题】:Why is my MVC viewModel null?为什么我的 MVC viewModel 为空?
【发布时间】:2010-10-02 00:41:37
【问题描述】:

我正在尝试将 viewModel 传递给编辑表单视图。问题是我实例化了模型并为其赋值,但模型仍然为空。

我的行动:

    public ActionResult OrderEdit(takeOrder FormInput)
    {
        takeOrder viewModel = new takeOrder
        {
            Name= "anonymous",
            TableNumber = 13,
            FoodItems = new List<FoodItem> {
                new FoodItem{ DishName = "Dishname value 1", Price = 10 },
                new FoodItem{ DishName = "Dishname value 2", Price = 20 },
                new FoodItem{ DishName = "Dishname value 3", Price = 30 },
                new FoodItem{ DishName = "Dishname value 4", Price = 40 },
            }
        };
        if (FormInput != null)
            viewModel = FormInput;

        return View(viewModel);
    }

我的观点是这样的:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/genesis.Master" Inherits="System.Web.Mvc.ViewPage<Genesis_0_02.Models.takeOrder>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    OrderEdit
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
            <h2>OrderEdit</h2>
    <% using (Html.BeginForm())
       { %>
        <%: Html.ValidationSummary() %>
        <p>
            <%: Html.LabelFor(x => x.Name) %><br />
            <%: Html.TextBoxFor(x => x.Name) %>
        </p>
        <p>
            <%: Html.LabelFor(x => x.TableNumber) %><br />
            <%: Html.TextBoxFor(x => x.TableNumber) %>
        </p>
        <button type="button" id="AddListItem">Add a dish to your order</button>
        <br />
        <% for (int i = 0; i < Model.FoodItems.Count(); i++ )%>
        <% { %>
        <div class="ListItem">
        <button type="button" class="RemoveListItem">X</button>
            Dish: <%: Html.TextBoxFor(x => x.FoodItems[i].DishName, new { style = "width:60px;" } )%>
            Price: <%: Html.TextBoxFor(x => x.FoodItems[i].Price, new { style = "width:60px;" })%>
        </div>
        <% } %>
        <br />
<button type="submit">Submit Order</button>

    <% } %>

</asp:Content>

我得到的错误是:

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: source

Source Error: 


Line 64:         <button type="button" id="AddListItem">Add a dish to your order</button>
Line 65:         <br />
--> error --> Line 66:         <% for (int i = 0; i < Model.FoodItems.Count(); i++ )%>
Line 67:         <% { %>
Line 68:         <div class="ListItem">


Source File: pathtotheproject\Genesis.0.02\Genesis.0.02\Views\Admin\OrderEdit.aspx    Line: 66 

我在我的操作OrderEdit 中在viewModel 上添加了一个断点,并且所有值都作为空值传递给视图。我不明白这一点,因为我对每个参数的值进行了硬编码。

为什么我的viewModel 为空?

编辑

我确实有 javascript 可以在表单中添加菜肴。我从上面列出的视图中删除了它,因为它与错误无关。

编辑:回复@Loki Stormbringer 的回答

public ActionResult OrderEdit(takeOrder FormInput) 中的 FormInput 参数旨在绑定到表单。

当我注释掉时:

        // if (FormInput != null)
        //    viewModel = FormInput;

然后代码按预期执行。但是我不明白为什么在我专门检查对象不为空时分配了null 对象。预期的行为是,如果已提交表单,则为 viewModel 分配已编辑的值,如果没有表单提交后,则 viewModel 使用默认值。最终,这些默认值将来自数据库。

为什么FormInput 为空时if (FormInput != null) 返回true?

【问题讨论】:

  • 我不能更强烈地提倡你改变你的编码风格。它与驼峰式类名和大写参数名的标准风格完全相反。 (应该反过来)
  • 就错误本身而言,您对每个参数进行了硬编码,但随后您继续使用if (FormInput != null) viewModel = FormInput; 覆盖该工作。大概FormInput不为null,但里面的所有属性都是null。这是回发的结果吗?为什么您希望FormInput 拥有有效数据?
  • @Kirk Woll - 我不知道有这样的约定。
  • @Kirk Woll - 感谢您提供资源!我得养成一些新习惯。

标签: c# asp.net-mvc asp.net-mvc-2


【解决方案1】:

不确定是否已注意到这一点,但根据该错误,您的模型 not 实际上为 null; ArgumentNullException。如果 Model 为空,则为 NullReferenceException

ArgumentNullException 表示Model.FoodItems 集合在传递给Count() 扩展方法时为空。

【讨论】:

  • 我没有注意到这一点。谢谢。
【解决方案2】:

FormInput 参数的值是多少?你怎么称呼这个动作?我的猜测是 FormInput 里面有一些东西并且不为空所以这条线

如果(表单输入!= null) 视图模型 = 表单输入;

被执行,然后吹走你放在上面 viewModel 中的任何东西。

【讨论】:

  • 感谢您的回答。我注释掉了 if 语句,它按预期工作。但是,这对我来说没有意义,因为只有在 FormInput 不为空时才应该分配 viewModel = FormInput。所以不知何故,if语句说FormInput不是空的,但赋值认为FormInput是空的。我一定在这里遗漏了什么。
【解决方案3】:

您的页面标题中有以下内容...您的视图模型类明确命名为“takeOrder”

Inherits="System.Web.Mvc.ViewPage<Genesis_0_02.Models.takeOrder>

编辑 我刚刚重读了您的帖子,这是您的模型名称...首先要重命名为 TakeOrder

【讨论】:

  • 感谢 Matt,@Kirk Woll 还指出我在命名对象和参数时缺乏约定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-29
  • 2021-05-29
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
  • 2018-11-22
  • 2011-01-06
相关资源
最近更新 更多