0
votes

I am trying to pass my items in the cart to paypal and am receiving the following error:

Object reference not set to an instance of an object.

My code is:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> Address and Payment

<h1>Checkout with PayPal</h1>

<form id="PayPal" name="PayPal" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" />

<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="upload" value="1" />
<input type="hidden" name="business" value="[email protected]" />

<% foreach (var item in Model.CartItems)
   { %>
<%=Html.Hidden("item_name" + item.Count.ToString(), item.Design.Title)%>
<%=Html.Hidden("amount" + item.Count.ToString(), item.Design.Price)%>
<%=Html.Hidden("quantity" + item.Count.ToString(), item.Count.ToString())%>
<%=Html.Hidden("shipping" + item.Count.ToString(), 0)%>
<%=Html.Hidden("handling" + item.Count.ToString(), 0)%>
<% } %>
<input type="image" src="https://fpdbs.paypal.com/dynamicimageweb?cmd=_dynamic-image" align="left" />

The error is highlighting the foreach item in Model.CartItems - however on the previous page this item is not null. Seems that item is coming up as null.

1
Can you please post your code for the Action methods in the controller for both pages.Joel Cunningham

1 Answers

0
votes

Make sure that you are passing the model in the controller action rendering this view and that the CartItems property of this model is not null:

public ActionResult Foo()
{
    ShoppingCartViewModel model = FetchYourModelFromSomewhere();
    // at this stage model and model.CartItems should not be null
    return View(model);
}

I don't know what do you mean when saying that the model is not null on the previous page but the controller action should always pass a model to the view.