他們投票後(我假設發生在POST),你要設置Cookie:
[HttpPost]
public ActionResult Vote()
{
HttpCookie hasVoted = new HttpCookie("hasVoted", true);
Request.Cookies.Add(hasVoted);
return RedirectToAction("Index");
}
得到它是這樣的:
[HttpGet]
public ActionResult Index()
{
bool hasVoted = Request.Cookies["hasVoted"].Value;
return View();
}
如果我這樣做,我會採取hasVoted布爾,怪人它在一個視圖模型,並設置按鈕的基礎上在.aspx頁面中值的可見性。如果你真的想雖然,你可以閱讀和使用javascript寫的cookie:
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
檢查了這一點http://www.quirksmode.org/js/cookies.html