I want to update textbox's value(that contains cookie's value) using Ajax in asp.net MVC5 . I'm very new in JavaScript and I wrote these codes , but my code didn't work . I didn't get any error but it's not working. I wrote JavaScript in foreign file 'UpdateTxtBox.js' and I added <script src="~/Scripts/UpdateTxtBox.js"></script> to Layout . Could anyone tell me what's the problem ?

$(function () {
$("textCountProduct").change(function () {
    var count = $(this).val();
    var id = $(this).attr("productid");
    $.ajax({
        url: "/Goods/AddToCart",
        data: { Id: id, Count: count },
        type: "Post",
        dataType: "Json",
        success: function (result) {
            if (result.Success) {
                alert(result.Html);
                $("#CartItems").html(result.Html);
            }
            eval(result.Script);
        },
        error: function () {
            alert("error....");
        }
    });
});
});

a part of Basket.cshtml

@using (Html.BeginForm("AddToCart", "Goods", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
   @Html.TextBoxFor(modelItem => item.Count, new { @class="text textCountProduct" , style="width:40px;" , productid=item.GoodDetails.DetailsGoodID})

 }

Good controller

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 = MessageBox.Show("Good added successfully", MessageType.Success).Script,
                //Script = "alert('Good added successfully');",
                Html = "cart items  (" + CartCount.ToString() + ")"
            }
                );
        }

Related posts

Recent Viewed