2013-04-05 69 views
2

我有一个文本框和一个提交按钮被放在那里输入一个5号码的针,然后它将在cookie内保存4个月,但它不工作我没有得到任何东西它..什么可能是错误的。这是我第一次尝试饼干mvc3 Cookie值未在ViewData页面设置

读查看

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

你一定要试试这个

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


    coo["Country"] = "INDIA"; 


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