代码之家  ›  专栏  ›  技术社区  ›  mare

MVC JSON操作返回bool

  •  8
  • mare  · 技术社区  · 15 年前

    我的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值上),所以thay返回 true false (在库特斯)但我有点不喜欢。

    其他人如何处理这样的案件?

    1 回复  |  直到 12 年前
        1
  •  16
  •   Darin Dimitrov    15 年前

    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 IsCalculateTax