Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7855513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:24:35+00:00 2026-06-02T20:24:35+00:00

I have a Controller and View which allow a user to ‘checkout’ the contents

  • 0

I have a Controller and View which allow a user to ‘checkout’ the contents of a shopping cart.

All was working fine when my CheckoutController was returning an Order model to the Checkout view. However, I now want to also display the contents of the shopping cart on the Checkout view so have modified the Controller and View to use a viewmodel containing the Order and CartItems.

I have created a CheckoutViewModel and modified the GET: and POST: ActionResults to use this viewmodel.

Problem is the CheckOutViewModel is not being properly populated during the POST:

I am getting an ‘Object reference not set to an instance of an object’ error on this line ‘checkoutViewModel.Order.Username = User.Identity.Name;’ in the POST: ActionResult but I am not sure exactly what is not being instansiated.

If need be, I can post the original working coded that used the Oreder model instead of the CheckoutViewModel.

I’m sure I have some simple sturctural or syntax problem, I just can’t tell what it is because I’m still trying to get my head around the best way to do things in MVC.

GET: ActionResult

    //GET: /Checkout/AddressAndPayment
    public ActionResult AddressAndPayment()
    {
        //To pre-populate the form, create a new Order object and get the ShoppingCart, populate the ViewModel and pass it to the view
        var order = new Order();
        order.Username = User.Identity.Name;
        MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
        storeDB.SaveChanges();

        var cart = ShoppingCart.GetCart(this.HttpContext);

        // Set up our ViewModel
        var viewModel = new CheckoutViewModel
        {
            CartItems = cart.GetCartItems(),
            CartTotal = cart.GetTotal(),
            Order = order
        };

        // Return the view
        return View(viewModel);
    }  

POST: ActionResult

        //POST: /Checkout/AddressAndPayment
    [HttpPost]
    public ActionResult AddressAndPayment(FormCollection values)
    {
        var checkoutViewModel = new CheckoutViewModel();
        TryValidateModel(checkoutViewModel);

        try
        {
            checkoutViewModel.Order.Username = User.Identity.Name;
            checkoutViewModel.Order.OrderDate = DateTime.Now;
            //Save Order
            storeDB.Orders.Add(checkoutViewModel.Order);
            storeDB.SaveChanges();
            //Process the order
            var cart = ShoppingCart.GetCart(this.HttpContext);
            cart.CreateOrder(checkoutViewModel.Order);
            storeDB.SaveChanges();
            //Send 'Order Confirmation' email
            ViewData["order"] = checkoutViewModel.Order;
            UserMailer.OrderConfirmation(checkoutViewModel.Order).SendAsync();

            return RedirectToAction("Complete", new { id = checkoutViewModel.Order.OrderID });
        }
        catch
        {
            //Invalid - redisplay with errors
            return View(checkoutViewModel);
        }
   }  

View

@model OrderUp.ViewModels.CheckoutViewModel
@{
    ViewBag.Title = "AddressAndPayment";
}
<script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) { 
@Html.ValidationSummary(true)
<h2>Pick Up Details</h2>
<fieldset>
    <legend>When are you gonna be hungry?</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Order.Phone)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Order.Phone)
        @Html.ValidationMessageFor(model => model.Order.Phone)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Order.PickUpDateTime)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Order.PickUpDateTime)
        @Html.ValidationMessageFor(model => model.Order.PickUpDateTime)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Order.Notes)
    </div>
    <div class="editor-multiline-field">
        @Html.EditorFor(model => model.Order.Notes)
        @Html.ValidationMessageFor(model => model.Order.Notes)
    </div>
</fieldset>
<input type="submit" value="Submit Order" />
}
<div style="height:30px;"></div>
<h3>
    <em>Review</em> your cart:
 </h3>
<table>
<tr>
    <th>
        Menu Item
    </th>

    <th>
        Price (each)
    </th>
    <th>
        Notes
    </th>
    <th>
        Quantity
    </th>
    <th></th>
</tr>
@foreach (var item in Model.CartItems)
{
    <tr id="row-@item.RecordID">
        <td>
            @Html.ActionLink(item.MenuItem.Name, "Details", "Store", new { id = item.MenuItemID }, null)
        </td>

        <td>
            @Html.DisplayFor(modelItem => item.MenuItem.Price)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Notes )
        </td>
        <td id="item-count-@item.RecordID">
            @item.Count
        </td>
        <td>
        </td>
    </tr>
}
<tr>
    <td >
        Total
    </td>
    <td  id="cart-total">
        @String.Format("${0:F2}", Model.CartTotal)
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
</tr>
</table>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:24:37+00:00Added an answer on June 2, 2026 at 8:24 pm

    You should use CheckoutViewModel as an input parameter to your HttpPost method and then try debug and see if you are still not getting that model populated after the form post.

    [HttpPost]
        public ActionResult AddressAndPayment(CheckOutViewModel checkoutViewModel)
        {
            TryValidateModel(checkoutViewModel);
    
            try
            {
                checkoutViewModel.Order.Username = User.Identity.Name;
                checkoutViewModel.Order.OrderDate = DateTime.Now;
                //Save Order
                storeDB.Orders.Add(checkoutViewModel.Order);
                storeDB.SaveChanges();
                //Process the order
                var cart = ShoppingCart.GetCart(this.HttpContext);
                cart.CreateOrder(checkoutViewModel.Order);
                storeDB.SaveChanges();
                //Send 'Order Confirmation' email
                ViewData["order"] = checkoutViewModel.Order;
                UserMailer.OrderConfirmation(checkoutViewModel.Order).SendAsync();
    
                return RedirectToAction("Complete", new { id = checkoutViewModel.Order.OrderID });
            }
            catch
            {
                //Invalid - redisplay with errors
                return View(checkoutViewModel);
            }
       }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view controller which contains a field for user to enter date
i have view controller which contains a button which show the image library ,if
I have a table view controller which doesn't let me manually scroll to the
Hi guys i have controller which returns a partial view, the controller is called
I have a navigation controller which also has a table view. I want to
In my IB I have a navigation controller which has in it a view
I have a app, in which I want the user to allow to download
I have a controller called user which just loads the user profile page for
I have a view controller which contains a table view, and which is wrapped
I have a modal view which gets the user to select some data to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.