I'm creating a shopping cart for my project in C# using MVC 4.
I stored 3 values: name, price and description as an array in a session.
When I try to call the Session for a foreach loop, it says
"foreach statement cannot operate on variable of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'"
What would I have to change?
Any help would be greatly appreciated.
Thank you
Courses.cshtml
@model IEnumerable<ANON_Capstone.Models.Course>
@if (!User.Identity.IsAuthenticated)
{
@section featured
{
<section class="featured">
<div class="content-wrapper">
<h2 class="text-center">Please Login or Create an Account to make a Purchase!</h2>
</div>
</section>
}
}
<div class="row">
<div class="col-lg-9">
<h2><strong>Courses</strong></h2><br />
@foreach (var item in Model)
{
<div class="col-md-4 col-xs-6 col-s-8 col-lg-4">
@using (Html.BeginForm("ShoppingCart", "Home"))
{
<img src="~/Images/party.gif" style="width: 175px" class="img-responsive" />
<h2>@item.className</h2>
<p>[email protected] -- @item.maxStudent spots left! </p>
<input type="text" name="product" value="@item.className" hidden="hidden" />
<input type="text" name="totalPrice" value="@item.classPrice" hidden="hidden" />
<input type="text" name="custom" value="@item.ClassID" hidden="hidden" />
if (User.Identity.IsAuthenticated)
{
<input class="btn btn-default" type="submit" name="btnConfirm" value="Add to Shopping Cart" />
}
}
<br />
</div>
}
</div>
</div>
HomeController.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ANON_Capstone.Models;
namespace ANON_Capstone.Controllers
{
public class HomeController : Controller
{
private UsersContext db = new UsersContext();
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
public ActionResult Contact()
{
return View();
}
public ActionResult Courses()
{
return View(db.Courses.ToList());
}
[HttpPost]
public ActionResult ShoppingCart()
{
return View();
}
public ActionResult ShoppingCart(string className, string classPrice, string classDesc)
{
String[] cart = { className, classPrice, classDesc };
Session["Cart"] = cart;
return View();
}
}
}
ShoppingCart.cshtml
@{
ViewBag.Title = "ShoppingCart";
}
<h2>ShoppingCart</h2>
@using (Html.BeginForm("ValidateCommand", "PayPal"))
{
var cart = Session["Cart"];
<div class="row">
<div class="col-lg-9">
<h2><strong>Courses</strong></h2><br />
<div class="col-md-4 col-xs-6 col-s-8 col-lg-4">
@foreach (var item in cart)
{
<img src="~/Images/party.gif" style="width: 175px" class="img-responsive" />
<h2>@item.className</h2>
<input type="text" name="product" value="@item.className" hidden="hidden" />
<input type="text" name="totalPrice" value="@item.classPrice" hidden="hidden" />
<input type="text" name="custom" value="@item.ClassID" hidden="hidden" />
}
<br />
</div>
</div>
<input class="btn btn-default" type="submit" name="btnConfirm" value="Check Out with Paypal" />
</div>
}
objectout of the Session..objectisn'tIEnumerable..