views:

63

answers:

3

I'm trying to pass a viewModel to an edit form view. The issue is that I instanciate the model and assign values to it, but the model remains null.

My Action:

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

And my view looks like this:

<%@ 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>

The error I get is:

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 

I added a breakpoint on viewModel in my Action OrderEdit and all of the values were being passed as null to the view. I don't understand this because I hardcoded values for every parameter.

Why is my viewModel null?

edit

I do have javascript that adds dishes to the form. I took that out of the view listed above because it isn't relevent to the error.

edit: response to @Loki Stormbringer's answer

The FormInput paramter in public ActionResult OrderEdit(takeOrder FormInput) is intended to be bound to a formpost.

When I comment out:

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

Then the code performs as expected. But I don't understand why a null object is being assigned when I am specifically checking that the object is not null. The intended behavior is that if a form has been submitted then the viewModel is assigned the edited values, and if there is no form post submission then the viewModel uses the default values. Eventually, those default values will be coming from a db.

Why does if (FormInput != null) return true when FormInput is null?

+1  A: 

What is the value of the FormInput parameter? How are you calling this Action? My guess is that FormInput has something in it and is not null so the line

if (FormInput != null) viewModel = FormInput;

is executed which then blows away anything you put in viewModel above.

Loki Stormbringer
Thank you for the answer. I commented out the if statement and it worked as expected. But, this doesn't make sense to me because `viewModel = FormInput` should only be assigned if `FormInput` is NOT null. So somehow the if statement says `FormInput` is not null but the assignment thinks that `FormInput` is null. I must be missing something here.
quakkels
Please see the edit in my question
quakkels
A: 

You have below in your Page header... is your viewmodel class definitely named "takeOrder"

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

Edit I just reread your post and it is your Model name... first thing would be to rename to TakeOrder

Matt
Thanks Matt, @Kirk Woll also pointed out my lack of convention when naming objects and parameters.
quakkels
+1  A: 

Not sure if this has been noticed, but your Model is not actually null according to that error; ArgumentNullException. It would be NullReferenceException if Model was null

ArgumentNullException means that the Model.FoodItems collection is null when being passed to the Count() Extension method.

Andrew Barber
I hadn't noticed that. Thanks.
quakkels