MVC JSON actions returning bool

前端 未结 1 503
逝去的感伤
逝去的感伤 2020-12-30 11:36

I had my ASP.NET MVC actions written like this:

    //
    // GET: /TaxStatements/CalculateTax/{prettyId}
    public ActionResult CalculateTax(int prettyId)
         


        
相关标签:
1条回答
  • 2020-12-30 12:19

    I would use anonymous object (remember that JSON is a key/value pairs):

    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
        );
    }
    

    And then:

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

    Remark: if the selected.calculateTax property is boolean the .NET naming convention would be to call it IsCalculateTax.

    0 讨论(0)
提交回复
热议问题