我想在提交到 cookie 时将复选框的状态保存在 html 表单中,然后根据这些 cookie 值启用/禁用我网站的其他部分。
我从这样的代码开始:
HTML:
<form method="POST">
<fieldset>
<legend>(Not really) Opt In or Out</legend>
<input type="checkbox" id="selectTwitter" name="selectTwitter" >I'm Twitterpated!</input><br />
. . .
剃刀:
@{ var twitterSelected = false; . . .
if (IsPost)
{
twitterSelected = Request["selectTwitter"].AsBool();
Response.Cookies["TwitterSelected"].Value = twitterSelected.ToString(); // Doesn't seem to accept saving boolean vals - saved as "false" or "true"?
Response.Cookies["TwitterSelected"].Expires = DateTime.Now.AddYears(1);
. . .
...但我坚持如何根据可能存在的 cookie val 检查或取消选中复选框:
if (Request.Cookies["selectTwitter"] != null)
{
// what now?
}