我正在使用 MVC 4 在 C# 中为我的项目创建购物车。
我在 session 中以数组形式存储了 3 个值:名称、价格和描述。
当我尝试调用 foreach 循环的 Session 时,它显示
“foreach 语句无法对‘object’类型的变量进行操作,因为‘object’不包含‘GetEnumerator’的公共(public)定义”
我需要改变什么?
任何帮助将不胜感激。
谢谢
类(class).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><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9fd99b0adbcb4f7bab5b8aaaa89abb0babc" rel="noreferrer noopener nofollow">[email protected]</a> -- @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>
}
最佳答案
您似乎想要在购物车中收集订单商品。
要实现这一点,请执行类似的操作
public ActionResult ShoppingCart(string className, string classPrice, string classDesc)
{
Session["Cart"] = db.Orders.ToList();
return View();
}
然后使用
var cart = Session["Cart"] as IEnumerable<Order>;
在您的 View 中访问这些项目
关于c# - 使用 MVC 4 在 C# 中使用 Session 进行 foreach 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20061626/