我创建了一个 cookie,用于保存复选框的最后更改状态。cookie 有效,我已经通过在警报框中传递所述 cookie 的值来证明这一点。cookie 还保存站点上复选框的状态。我需要将该 cookie 的值或当前复选框值传递给控制器,以便我可以相应地更改 JQuery 数据表。任何帮助表示赞赏。
这是我的视图脚本:
$(function () {
var oTable = $('#myIssuesTable').dataTable({
"bProcessing": true,
"bServerSide": false,
"bJQueryUI": true,
"sAjaxSource": '@Url.Action("GetIssuesViaAjax", "Exchange")'
});
$('#activeOnly').click(function () {
var activeStatus = $('#activeOnly:checked').val() ?true:false;
$.cookie("showActiveIssues", activeStatus, { expires: 365 }, { path: '/' });
alert($.cookie("showActiveIssues"))
//if (navigator.cookieEnabled)
// alert("ON");
//else
// alert("OFF");
@*$.post("@Url.Action("GetIssuesViaAjax", "Exchange")", { test: activeStatus });*@
@*$.ajax({
url: '@Url.Action("GetIssuesViaAjax", "Exchange")',
type: 'POST',
//dataType: "json",
//contentType: 'application/json',
//traditional: true,
data: {test:"Testarooskie"}*@
});
oTable.fnReloadAjax('@Url.Action("GetIssuesViaAjax", "Exchange")');
});
$('#activeOnly').ready(function ()
{
//alert($.cookie("showActiveIssues"))
var cbCookie = $.cookie("showActiveIssues")
if (cbCookie == 1)
{
//alert("working correctly")
$('#activeOnly').prop('checked', true)
}
else {
//alert("working incorrectly")
$('#activeOnly').prop('checked', false)
}
});
});
</script>
@Html.CheckBox("activeOnly")
@Html.Label("Active Only")</>
我的控制器:
public class ExchangeController : MultiBase
{
ITExchangeReposity hufDalRepository = new ITExchangeReposity();
SecurityReposity hufDalSecurityRepository = new SecurityReposity();
[HttpPost]
public ActionResult GetIssuesViaAjax(jQueryDataTableParamModel param)
{
//HttpCookie test = Request.Cookies["showActiveIssues"];
//Convert.ToBoolean(HttpContext.Request.Cookies["showActiveIssues"]);
bool cbValue = Convert.ToBoolean(Request.Cookies["showActiveIssues"]);//returns only default value of "false"
//string cbValue = Convert.ToString(Request.Cookies["showActiveIssues"]);//returns an empty value ("").
var issues = hufDalRepository.FindAllIssues(2 , cbValue);
var result = from c in issues
select new[] { c.IDTSSubmitIssue.ToString(), c.Subject, c.IDUser.ToString(), c.ReviewedBy, c.StatusDescription, c.DateSubmitted.ToShortDateString() };
return Json(new
{
sEcho = param.sEcho,
iTotalRecords = issues.Count(),
iTotalDisplayRecords = issues.Count(),
aaData = result
},
JsonRequestBehavior.AllowGet);
}