【问题标题】:MVC Controller Shopping Cart adding to a listMVC 控制器购物车添加到列表
【发布时间】:2013-11-19 15:04:23
【问题描述】:

我正在尝试制作购物车,但在将商品添加到购物车时遇到问题。

我正在尝试将商品添加到购物车,但似乎每次我调用控制器的 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);
    }
}
}

课程.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; }
}
}

【问题讨论】:

    标签: c# list asp.net-mvc-4 shopping-cart


    【解决方案1】:

    您只是在每次页面调用时创建 Carts 和 CartsModel 的新实例,而不是从数据库中加载它们。

    基于 Web 的应用程序是无状态的,它们不会跨请求记住变量。

    您需要编写一些代码来跨多个请求持久化和加载您的购物车。

    【讨论】:

    • 您好,谢谢您的回答。在我的家庭控制器中,我确保在控制器之外创建一个 Carts 实例。每次我访问家庭控制器上的页面时,它是否仍会实例化它?如果是这样,我应该使用什么方法来修复它?谢谢
    • 是的,每次都是一个新实例。您需要在每次调用 ShoppingCart 方法结束时为您的购物车提供一个 ID 并将其保存到数据库中。您还需要在 ShoppingCart 方法中添加一个 CartId 参数,以便在每次调用时从数据库中加载正确的购物车。
    【解决方案2】:

    解决方案是将您的 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>
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-12
      • 2015-10-10
      • 1970-01-01
      相关资源
      最近更新 更多