MVC JSON动作返回bool

我的ASP.NET MVC操作编写如下:

// // GET: /TaxStatements/CalculateTax/{prettyId} public ActionResult CalculateTax(int prettyId) { if (prettyId == 0) return Json(true, JsonRequestBehavior.AllowGet); TaxStatement selected = _repository.Load(prettyId); return Json(selected.calculateTax, JsonRequestBehavior.AllowGet); // calculateTax is of type bool } 

我有这个问题因为在jquery函数中使用它时我遇到了各种错误,主要是toLowerCase()函数失败。

所以我必须以一种方式更改动作,它们将bool作为字符串返回ToString()在bool值上调用ToString() ),以便返回truefalse (在qoutes中),但我有点不喜欢它。

其他人如何处理这种情况?

我会使用匿名对象(请记住JSON是键/值对):

 public ActionResult CalculateTax(int prettyId) { if (prettyId == 0) { return Json( new { isCalculateTax = true }, JsonRequestBehavior.AllowGet ); } var selected = _repository.Load(prettyId); return Json( new { isCalculateTax = selected.calculateTax }, JsonRequestBehavior.AllowGet ); } 

然后:

 success: function(result) { if (result.isCalculateTax) { ... } } 

备注:如果selected.calculateTax属性为boolean,则.NET命名约定将称为IsCalculateTax