在ViewData页面中未设置mvc3 cookie值

时间:2013-04-05 00:23:46

标签: asp.net-mvc-3 cookies httpresponse

我有一个文本框和一个提交按钮,人们放在那里输入一个5号码针然后它是在一个cookie内安全4个月,但它没有工作我什么都没有得到它...什么可能是错的..这是我第一次尝试使用cookies

阅读视图

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"]

1 个答案:

答案 0 :(得分:1)

你必须尝试这个

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


    coo["Country"] = "INDIA";


    ViewData["prefvalue"] = coo["Country"];
    return View();
}