Skip to main content
 首页 » 编程设计

c#之为什么我的 MVC viewModel 为空

2024年02月24日22mengfanrong

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

我的行动:

    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); 
    } 

我的 View 是这样的:

<%@ 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 传递给 View 。我不明白这一点,因为我对每个参数的值进行了硬编码。

为什么我的 viewModel 为空?

编辑

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

编辑:对@Loki Stormbringer 的回答的回应

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

当我注释掉时:

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

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

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

请您参考如下方法:

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

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