asp.net mvc中cookie的完整理解

情形描述:

客户填表,提交,jquery把数据异步提交到服务器,服务器通过Request.Form["key"]得到数据,判断Request.Cookie里边是否存在cookie,如果存在,写入,否则,do nothing

测试代码如下:

    public ActionResult Index()
        {
            var cookie = Request.Cookies["userinfo"];
            return View();
        }

 

 

     public ActionResult SaveCookie()
        {
            HttpCookie _userInfoCookies = new HttpCookie("userinfo");
            _userInfoCookies["UserName"] = "Abhijit";
            _userInfoCookies["PassWord"] = "RedGreen";
            Response.Cookies.Add(_userInfoCookies);
            return View();
        }

cshtml文件:

@model JQueryMVC.Models.UserInfo

@{     Layout = null; }

<!DOCTYPE html>

<html> <head>     <title>SaveCookie</title> </head> <body>     <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>     <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>     <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

 @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>UserInfo</legend>
   
            <div class="editor-label">
                @Html.LabelFor(model => model.UserName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.UserName)
                @Html.ValidationMessageFor(model => model.UserName)
            </div>
   
            <div class="editor-label">
                @Html.LabelFor(model => model.PassWord)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.PassWord)
                @Html.ValidationMessageFor(model => model.PassWord)
            </div>
   
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>

 }
   
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
</body>
</html>

模型文件:

   public class UserInfo   

    {     

        private string username;   

        private string password;

         [Display(Name = "用户名")]  

       [Required(ErrorMessage = "*姓名必填")]     

       [StringLength(5, ErrorMessage = "*长度必须大于5")]

   

        public string UserName      

      {             get           

  {                 return username;       

      }

            set     

        {                 username = value;       

      }

        }    

     [Display(Name = "密码")]     

    [Required(ErrorMessage = "*密码必填")]

        public string PassWord  

       {          

   get        

     {                 return password;        

     }

            set     

        {           

      password = value;      

       }       

  }

    }

Jquery代码:

 <button id="button" value="ajax" />
    <script type="text/javascript">
        $(document).ready(function () {
            $("#button").click(function () {
                alert("准备 ajax");
                $.ajax({
                    type: "POST",
                    url: "http://localhost:56167/My/Delete",
                    success: function (data) {
                       alert("ajax 调用成功,data is" + data);
                    },

  data: {
                        "people": [
                        { "firstName": "Brett", "lastName": "McLaughlin", "email": "aaaa" },
                        { "firstName": "Jason", "lastName": "Hunter", "email": "bbbb" },
                        { "firstName": "Elliotte", "lastName": "Harold", "email": "cccc" }
                        ]
                    },
                    error: function (data) {
                        alert("This user cannot be deleted");
                    }
                });
            });
        });
    </script>

hope my article will help you,thanks

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。