2013-04-05 70 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(); 
}