存储从html到cookie的更改

时间:2014-03-02 08:06:28

标签: c# jquery asp.net cookies shopping-cart

我有购物车商品的代码。数据从网站cookie中检索,'MyKookie'

  <asp:ListView ID="List" runat="server" DataKeyNames="ID">
   >
<div class="margin">
    <table class="tbl">
        <tbody>
            <tr>
                <table>
                    <tbody>
                        <tr>
        <td class="one">
          <h4><%# Eval("ID")%></h4>
       </td>
       <td class="two">
       <h4><%# Eval("Name")%></h4>
         </td>
                            <td class="fone">9960</td>

                            <td>
                                <select class='drop'>
                                    <option value="1">1</option>
                                    <option value="2" selected="selected">2</option>
                                    <option value="3">3</option>
                                    <option value="4">4</option>
                                </select>
                            </td>
                            <td class="fthree">20</td>
                            <td class="ffour">5000</td>
                        </tr>
                    </tbody>
                </table>
            </tr>
        </tbody>
    </table>

 </asp:ListView>

当用户更改一个项目的数量时,如何在MyCookie中存储更改?

1 个答案:

答案 0 :(得分:0)

在客户端使用jquery.cookie.js。这很容易:

$.cookie('cookie_name', 'cookie_value', {
expires: 5,
path: '/',
domain: 'subdomain.yoursite.ru',
secure: true

});

如果你需要在服务器端:

var cookie = new HttpCookie("CookieName")
            {
                Value = JsonConvert.SerializeObject(model), // if value is object, else use simple string
                Expires = DateTime.Now.AddYears(1)
            };

            Response.Cookies.Add(cookie);

用于确定Cookie值:

var model = JsonConvert.DeserializeObject<ModelType>(Request.Cookies["CookieName"].Value); // or Request.Cookies["CookieName"].Value if simple string