2

我有一个文本框和一个提交按钮,人们在其中输入一个 5 数字密码,然后将其保存在 cookie 中 4 个月,但它无法正常工作,我什么也没得到.. 可能有什么问题.. 这我是第一次尝试饼干吗

阅读视图

public ActionResult Index()
{
    //read cookie and send it to view model
    var mycookie = Request.Cookies["mypreference"];
    ViewData["prefvalue"] = mycookie.Value;
    return View();
}

HttpPost

[HttpPost]
public ActionResult Index(FormCollection ss)
{
    // create cookie
    HttpCookie preference = new HttpCookie("mypreference");
    preference.Value = ss["preffer"];
    preference.Expires = DateTime.Now.AddDays(120d);
    Response.Cookies.Add(preference);
    return View();
}

风景

@using (Html.BeginForm("seotips", "home", FormMethod.Post))
{
    @Html.TextBox("preffer") 
    <input type="submit" value="submit" />
}
@ViewData["prefvalue"]
4

1 回答 1

1

你必须试试这个

public ActionResult Index()
{
   HttpCookie coo = new HttpCookie("name");


    coo["Country"] = "INDIA";


    ViewData["prefvalue"] = coo["Country"];
    return View();
}
于 2013-04-05T05:14:59.570 回答