用会话控制购物车

时间:2015-10-04 17:58:46

标签: asp.net-mvc-5 session-cookies session-state

我的购物车在localhost中工作正常,但是当我在云端托管中托管时效果不好问题是,当我点击添加到购物车按钮时,它会添加一个产品,但是当我在购物车中添加另一个产品时再次它将覆盖前一个并且当我在最后添加时只显示购物车中的一个产品..我不知道会话的错误它会再次覆盖会话我想。另一个问题是我的更新购物车按钮的功能和删除按钮功能在购物车中无法正常工作

Object reference not set to an instance of an object.

我有一个控制器名称shoppingCartController这里是代码

namespace Medi.Areas.User.Controllers
{
public class ShoppingCartController : Controller
{
    ArrayList arr = new ArrayList();
    int id;


    BLL.IRepository<tbl_Product> de = new BLL.IRepository<tbl_Product>();
    public ActionResult Index()
    {
        return View();
    }
    private int isExisting(int id)
    {
        List<Items> cart = (List<Items>)Session["cart"];
        for (int i = 0; i < cart.Count; i++)
            if (cart[i].Pr.ProductID == id)
                return i;
        return -1;


    }
    public ActionResult Delete(int id)
    {
        int index = isExisting(id);
        List<Items> cart = (List<Items>)Session["cart"];
        cart.RemoveAt(index);
        Session["cart"] = cart;
        return PartialView("_pvCart");
    }
    public ActionResult OrderNow(string q)
    {
        if (Session["cart"] == null)
        {
            List<Items> cart = new List<Items>();
            cart.Add(new Items(de.GetById(Convert.ToInt32(q)), 1));
            Session["cart"] = cart;
            // ViewBag.quantity = new MultiSelectList(cart,"Quantity","Quantity");
            //  cart[1]

        }
        else
        {
            id = Convert.ToInt32(q);
            List<Items> cart = (List<Items>)Session["cart"];
            int index = isExisting(id);
            if (index == -1)
                cart.Add(new Items(de.GetById(id), 1));
            else
                cart[index].Quantity++;
            Session["cart"] = cart;
            // ViewBag.quantity = new MultiSelectList(cart, "Quantity", "Quantity");
        }

        return View("Cart");
    }
    [HttpPost]
    public ActionResult UpdateCart(int[] ProductID,int [] quantity )
    {
        int[] x = new int[4];
        int[] y = new int[4];
        List<Items> cart = (List<Items>)Session["cart"];
        for (int i = 0; i < cart.Count; i++)
        {
            x[i] = ProductID[i];
            y[i] = quantity[i];
            updcart(x[i],y[i]);
        }

        Session["cart"] = cart;
        return PartialView("_pvCart");

    }

    public void updcart(int id,int quantity) {
        List<Items> cart = (List<Items>)Session["cart"];
        int index = isExisting(id);
        if (index == -1)
            cart.Add(new Items(de.GetById(id), 1));
        else
            cart[index].Quantity = quantity;
        Session["cart"] = cart;
    }
}
 }

这里是视图名称Cart.cshtml

@{
ViewBag.Title = "Cart";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="~/Scripts/jquery-2.1.4.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>

 @using (Ajax.BeginForm("UpdateCart", "ShoppingCart", new AjaxOptions() {  HttpMethod = "POST", UpdateTargetId = "MyData", InsertionMode =  InsertionMode.Replace }))
{
@Html.AntiForgeryToken()
<br />
<br />
<div id="MyData">
    @{Html.RenderPartial("_pvCart");}
</div>
<br /><br />

<input id="updatecart" type="submit" value="update Cart" />

}

这是局部视图代码

@using Medi.Models;

<script src="~/Scripts/metro.min.js"></script>
<table class="table hovered" cellpadding=" 2" cellspacing="2" border="1px">
<tr class="info">
    <th></th>

    <th>Name</th>
    <th>Price</th>
    <th>Quantity</th>
    <th>Sub Total</th>
</tr>
@{
    decimal s = 0;
}
@foreach (Items item in (List<Items>)Session["cart"])
{
    <input id="ProductID" name="ProductID" type="hidden" value="@item.Pr.ProductID" />
    s = s + (Convert.ToInt32(item.Pr.Price) * item.Quantity);
    <tr>

        <th>@Ajax.ActionLink("Delete", "Delete", new { id = item.Pr.ProductID }, new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "MyData", InsertionMode = InsertionMode.Replace }, new { @class = "mif-cross" })</th>

        <td>@item.Pr.ProductName</td>
        <td>@item.Pr.Price</td>
        <td>
            <input name="quantity" id="quantity" type="text" style="width:25px;" value="@item.Quantity" />



        </td>
        <td>@(item.Pr.Price * item.Quantity)</td>
    </tr>
}
</table><br />
<hr />
<table cellpadding="1px" cellspacing="1px" style="float:right;">
<tr align="center" colspan="5" style="background-color:gray;">
    <td><h3 style="padding:1px;">Total</h3></td>
    <td> <h3 style="padding:1px;">Rs @s</h3></td>

</tr>
</table>

这是模型类

using BOL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

 namespace Medi.Models
{
 public class Items
{
    tbl_Product pr = new tbl_Product();

    public tbl_Product Pr
    {
        get { return pr; }
        set { pr = value; }
    }
    int quantity;

    public int Quantity { get; set; }
    public Items()
    {

    }
    public Items(tbl_Product product, int quantity)
    {
        this.Pr = product;
        this.Quantity = quantity;
    }
}
 }

我也在web.config中编写了这段代码

<httpCookies httpOnlyCookies="true" requireSSL="true"/>
<sessionState mode="InProc"/>

0 个答案:

没有答案