我正在尝试制作一个购物车,而我在购物车中添加商品时遇到了麻烦。
我正在尝试向购物车添加商品,但每次调用控制器的ShoppingCart部件时,它似乎都会自动刷新。
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();
Carts carts = new Carts();
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
public ActionResult Contact()
{
return View();
}
public ActionResult Courses()
{
return View(db.Courses.ToList());
}
public ActionResult ShoppingCart(string className, string classPrice, string classID, string classDesc)
{
if (carts.CartModels == null)
{
carts.CartModels = new List<CartModel>();
}
CartModel cart = new CartModel();
cart.className = className;
cart.classPrice = Convert.ToDouble(classPrice);
cart.classID = Convert.ToInt32(classID);
cart.classDesc = classDesc;
//if (className != null)
//{
// carts.CartModels.Add(new CartModel() { className = className, classPrice = Convert.ToDouble(classPrice), classID = Convert.ToInt32(classID), classDesc = classDesc });
//}
if (className != null)
{
carts.CartModels.Add(new CartModel() { className = className, classPrice = Convert.ToDouble(classPrice), classID = Convert.ToInt32(classID), classDesc = classDesc });
}
//Session["Cart"] = cart;
//ViewBag.Name = Session["Cart"] as ANON_Capstone.Models.Carts;
//ViewData["Cart"] = cart;
Console.WriteLine();
return View(carts);
}
}
}
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", FormMethod.Post))
{
<img src="~/Images/party.gif" style="width: 175px" class="img-responsive" />
<h2>@item.className</h2>
<p>$@item.classPrice -- @item.maxStudent spots left! </p>
<input type="text" name="className" value="@item.className" hidden="hidden" />
<input type="text" name="classPrice" value="@item.classPrice" hidden="hidden" />
<input type="text" name="classID" value="@item.ClassID" hidden="hidden" />
<input type="hidden" name="classDesc" value="@item.classDesc" />
if (User.Identity.IsAuthenticated)
{
<input class="btn btn-default" type="submit" name="btnConfirm" value="Add to Shopping Cart" />
}
}
<br />
</div>
}
</div>
</div>
ShoppingCart.cshtml
@model ANON_Capstone.Models.Carts
@{
ViewBag.Title = "Shopping Cart";
}
<h2>Shopping Cart</h2>
@using (Html.BeginForm("ValidateCommand", "PayPal"))
{
<div class="row">
<div class="col-lg-9">
@if (Model.CartModels.Count != 0)
{
<table border="1">
<tr>
<th>Course Image</th>
<th>Course Name</th>
<th>Course Desc</th>
<th>Course Price</th>
</tr>
@foreach (var item in Model.CartModels)
{
<tr>
<td><img src="~/Images/party.gif" style="width: 175px" class="img-responsive" /></td>
<td>@item.className</td>
<td>@item.classDesc</td>
<td>@item.classPrice</td>
</tr>
}
</table>
<input class="btn btn-default" type="submit" name="btnConfirm" value="Check Out with Paypal" />
}
else
{
<text>Your shopping cart is currently empty</text>
}
</div>
</div>
}
Carts.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace ANON_Capstone.Models
{
public class Carts
{
public List<CartModel> CartModels { get; set; }
public double CartTotal { get; set; }
}
}
CartModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace ANON_Capstone.Models
{
public class CartModel
{
public string className { get; set; }
public double classPrice { get; set; }
public int classID { get; set; }
public string classDesc { get; set; }
}
}
答案 0 :(得分:0)
您只是在每次页面调用时创建Carts和CartsModel的新实例,而不是从数据库中加载它们。
基于Web的应用程序是无状态的,它们不会记住请求中的变量。
您需要编写一些代码来保存并在多个请求中加载您的购物车。
答案 1 :(得分:0)
解决方案是将您的foreach
包含在“ IF”阻止案例中,而您的foreach
将从新的刷新中退出。
@if(Model.CartModels != null){
foreach (var item in Model.CartModels)
{
<tr>
<td><img src="~/Images/party.gif" style="width: 175px" class="img-responsive" /></td>
<td>@item.className</td>
<td>@item.classDesc</td>
<td>@item.classPrice</td>
</tr>
}
</table>
<input class="btn btn-default" type="submit" name="btnConfirm" value="Check Out with Paypal" />
}
else
{
<text>Your shopping cart is currently empty</text>
}
}