2016-01-13 102 views
1

我正在开发一个asp.net mvc 5在线商店项目。我想创建cart以添加商品Cookie。我很困惑,不知道为什么它不起作用。它没有给我任何错误。我还添加了断点来调试它,但是没有任何数据发送到我的操作! 任何人都可以帮助我吗?有什么问题 ? 我不是javascript好,我觉得问题就在javascript代码:提前/ 感谢为什么这些cookie代码在asp.net mvc中不起作用?

商品控制器

[HttpPost] 
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); 
      } 
      int CartCount = Request.Cookies.AllKeys.Where(p => p.StartsWith("NishtmanCart_")).Count(); 
      return Json(new MyJsonData() 
      { 
       Success = true, 
       Script = MessageBox.Show("product added to your basket", MessageType.Success).Script, 
       Html = "Shopping Cart (" + CartCount.ToString() + ")" 
      }); 
     } 
     catch (Exception) 
     { 
      return Json(new MyJsonData() 
      { 
       Success = false, 
       Script = MessageBox.Show("product didn't add to your basket", MessageType.Error).Script, 
       Html = "" 
      }); 
     } 
    } 

    public ActionResult RemoveCart(int Id) 
    { 

     try 
     { 
      int CartCount = Request.Cookies.AllKeys.Where(p => p.StartsWith("NishtmanCart_")).Count(); 

      if (Request.Cookies.AllKeys.Contains("NishtmanCart_" + Id.ToString())) 
      { 
       Request.Cookies["NishtmanCart_" + Id.ToString()].Expires = DateTime.Now.AddDays(-1); 
       return Json(new MyJsonData() 
       { 
        Success = true, 
        Script = MessageBox.Show("product removed from your basket", MessageType.Success).Script, 
        Html = "Shopping Cart (" + CartCount.ToString() + ")" 
       }); 
      } 
      else 
      { 
       return Json(new MyJsonData() 
       { 
        Success = false, 
        Script = MessageBox.Show("this product doesn't have in your basket", MessageType.Warning).Script, 
        Html = "Shopping Cart (" + CartCount.ToString() + ")" 
       }); 
      } 
     } 
     catch (Exception) 
     { 
      return Json(new MyJsonData() 
      { 
       Success = true, 
       Script = MessageBox.Show("product didn't remove from your basket", MessageType.Error).Script, 
       Html = "" 
      }); 
     } 

    } 

MyJsonData.cs

public class MyJsonData 
{ 
    public string Script { get; set; } 
    public string Html { get; set; } 
    public bool Success { get; set; } 
} 

_ GoodDetailsAjax.cshtml

@foreach (var item in Model.GoodDetails) 
{ 
    <div> 
     <p class="nowprice">NowPrice : @item.DetailsNowPrice</p> 
     <p class="preprice">PrePrice : @item.DetailsPrePrice</p> 
     <a class="button icon-cart" href="#" GoodID="@item.DetailsGoodID">Add to cart</a><br> 
     <a class="link" >Shopping Cart (0)</a> 
    </div> 
} 
@section scripts{ 
<script src="~/Scripts/jquery-2.1.4.min.js"></script> 
<script src="~/Scripts/bootstrap.min.js"></script> 
<script> 
    $(function() { 
     $("a.button.icon-cart").click(function (e) { 
      e.preventDefault(); 
      var goodId = $(this).attr("GoodID"); 
      alert(goodId); //////// I just added this code 
      $.ajax({ 
       url: "/Goods/AddToCart", 
       data: { Id: goodId, Count: 1 }, 
       type: "Post", 
       dataType: "Json", 
       success: function (result) { 
        if (result.Success) { 
         $("#CartItems").html(result.Html); 
        } 
        eval(result.Script); 
       }, 
       error: function() { 
        alert("Error!"); 
       } 
      }); 
     }); 
    }); 
</script> 
} 
+0

你用这段代码面临什么问题?你可以请更具体吗? –

+0

您正在返回JSON,我认为这意味着您正在发出AJAX请求。如果你想添加/删除cookie,你需要发出HTTP请求。 –

+0

它没有工作,当我点击'添加到购物车按钮'它应该添加1计数到'购物车'标记,但它没有@FrebinFrancis –

回答

0

我所有的代码都是真的,我只是犯了一些简单的错误。

我在我的布局中加载了一个JQuery文件,并且在我的视图中加载了另一个JQuery版本!我删除了其中一个。

而且我在部分视图中使用这些代码并使用Ajax加载它们,但是我的部分视图无法将数据传递到控制器,我将代码移动到主视图(GoodDetails.cshtml),现在它工作正常。

+0

很高兴听到你解决了这个问题:) –

+0

随着你的帮助...谢谢亲爱的@FrebinFrancis :) –

1

我不知道什么是的MessageBox.Show("....", MessageType.Error).Script实施,但我猜想,它只是产生一个简单的JavaScript语句是这样的:

Script = "alert('product added to your basket');" 

所以,你可以添加此标记结果:

<div id="CartItems"> 

</div> 

现在它没有任何问题。

相关问题