I'm developing an online store website using ASP.Net MVC 5 and Razor Engine . I have a view for show main goods then I used Ajax and partial view for show some info about it . and in partial view I have a add to cart button for add goods to cart using cookie . everything is OK but when I click another main good it didn't work correctly and value of Cart Items Does not increment.

I used CartCount action method for save cookie's value but it just worked when I refresh all of page . I don't know I explained my problem good or not ! Thanks in advance for any help.

GoodController

public PartialViewResult GoodDetailsAjax(int id)
    {
        GoodsVM vm = new GoodsVM();
        var GoodDetailsAjax = db.GoodDetails.Where(p => p.DetailsGoodID == id).ToList();
        vm.GoodDetails = GoodDetailsAjax;
        return PartialView("_GoodDetailsAjax", vm);
    }
    public ActionResult GoodDetails(int id)
    {
        GoodsVM vm = new GoodsVM();
        vm.SocialNetworks = blSocialNetwork.select();
        var goodDetails = db.GoodDetails.Where(p => p.FKSubGoods == id).ToList();
        vm.GoodDetails = goodDetails;
        vm.Footers = blFooter.select();
        vm.FooterMenus = blFooterMenu.select();
        return View(vm);

    }

public ActionResult AddToCart (int Id , int Count)
    {
        try
        {
            if (Request.Cookies.AllKeys.Contains("NishtmanCart_" + Id.ToString()))
        {
            //Edit cookie
            var cookie = new HttpCookie("NishtmanCart_" + Id.ToString(), (Convert.ToInt32(Request.Cookies["NishtmanCart_" + Id.ToString()].Value) + 1).ToString());
            cookie.Expires = DateTime.Now.AddMonths(1);
            cookie.HttpOnly = true;
            Response.Cookies.Set(cookie);

        }
        else
        {
            //Add new cookie
            var cookie = new HttpCookie("NishtmanCart_" + Id.ToString(), Count.ToString());
            cookie.Expires = DateTime.Now.AddMonths(1);
            cookie.HttpOnly = true;
            Response.Cookies.Add(cookie);
        }
            List<HttpCookie> lst = new List<HttpCookie>();
            for (int i = 0; i < Request.Cookies.Count; i++ )
            {
                lst.Add(Request.Cookies[i]);
            }

            bool isGet = Request.HttpMethod == "GET";
            int CartCount = lst.Where(p => p.Name.StartsWith("NishtmanCart_") && p.HttpOnly != isGet).Count();
            return Json(new MyJsonData()
            {
                Success = true,
                Script = "alert('Good added suucessfully');",
                Html = "Cart Items (" + CartCount.ToString() + ")"
            }
                );
        }
        catch(Exception)
        {
            return Json(new MyJsonData()
            {
                Success = false,
                Script = "alert('Good didn't add');",
                Html = ""
            }
                );

        }

    }
 public string CartCount ()
    {
        List<HttpCookie> lst = new List<HttpCookie>();
        for (int i = 0; i < Request.Cookies.Count; i++)
        {
            lst.Add(Request.Cookies[i]);
        }
        int CartCount = lst.Where(p => p.Name.StartsWith("NishtmanCart_") && p.HttpOnly == false).Count();
        return CartCount.ToString();
    }

_GoodDetailsAjax.cshtml

 @model NP1.ViewModels.GoodsVM
<div id="CartItems">
<a class="link" href="#">(@{ Html.RenderAction("CartCount", "Goods");})<span>Cart Items</span></a>
</div>
@foreach (var item in Model.GoodDetails)
   {
    //Products showed in carousel here ...
    <div>
        <p class="nowprice">NowPrice : @item.DetailsNowPrice</p>
        <p class="preprice">PrePrice : @item.DetailsPrePrice</p>
        <a class="button icon-cart" href="#" product="@item.DetailsGoodID">Add to cart</a><br>
    </div>
 }

GoodDetails.cshtml

 @model NP1.ViewModels.GoodsVM
 @{
 ViewBag.Title = "...";
 Layout = "~/Views/Shared/_Goods.cshtml";
  }
 @section scripts{
  <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
  <script src="~/Scripts/Magnify/jqzoom.pack.1.0.1.js"></script>
  <script src="~/Scripts/Noty/jquery.noty.js"></script>
  }
  <div style="background-color:#f7f7f7 ; width:100%;text-align:center;">
 <div class="div2" id="DivAjax" style="">

    @{Html.RenderAction("GoodDetailsAjax", new { id = Model.GoodDetails.FirstOrDefault().DetailsGoodID });}

 </div>

 <div class="div1" style="vertical-align:top;padding-left:1%;">
    @foreach (var item1 in Model.GoodDetails)
    {
        @Ajax.ActionLink(@item1.DetailsName, "GoodDetailsAjax", new { id = @item1.DetailsGoodID }, new AjaxOptions()
{
HttpMethod = "GET",
UpdateTargetId = "DivAjax",
InsertionMode = InsertionMode.Replace
}
)
<input type="image" class="img-responsive" src="~/Images/GoodDetails/@item1.DetailsSmallImage1">
    }
</div>

</div>

Related posts

Recent Viewed