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="rbur04_1291575642_biz@gmail.com" />
<% 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 开发者_如何学Pythonin Model.CartItems - however on the previous page this item is not null. Seems that item is coming up as null.
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.
精彩评论