繁体   English   中英

为什么我的MVC viewModel为null?

[英]Why is my MVC viewModel null?

我正在尝试将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 

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

为什么我的viewModel null?

编辑

我有javascript添加菜单到表单。 我把它从上面列出的视图中取出,因为它与错误无关。

编辑:回应@Loki Stormbringer的回答

public ActionResult OrderEdit(takeOrder FormInput)FormInput参数旨在绑定到formpost。

当我评论出来时:

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

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

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

不知道这是否已经注意到了,但你的模型是不是按照这个错误其实空; ArgumentNullException 如果Model为null,则为NullReferenceException

ArgumentNullException意味着Model.FoodItems集合在传递给Count() Extension方法时为null。

FormInput参数的值是多少? 你怎么称呼这个动作? 我的猜测是FormInput中有一些内容并且不是null,所以这一行

if(FormInput!= null)viewModel = FormInput;

执行后会将您放在viewModel中的任何内容吹走。

你的页面标题下面有...你的viewmodel类肯定名为“takeOrder”

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM