0
votes

I have a controller which return many datas. then i got "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property."

i have add my web.config using this. But the error is still occured.

  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="2147483645" recursionLimit="100">
        </jsonSerialization>
      </webServices>
    </scripting>
  </system.web.extensions>

then I add new class LargeJsonResult like on this website http://brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/

it said like this in controller

return new LargeJsonResult() { Data = output, MaxJsonLength = int.MaxValue };

but how can I use that with many return data? below is my controller

public ActionResult LoadInitData()
        {
            try
            {
                Database db = new Database("CON001");
                _employee = Helper.Common.GetEmployeeData(db);

                EmployeeDAC dacEmployee = new EmployeeDAC(db);
                Employee dataEmployee = dacEmployee.GetDataByComputerLogin(GetUser());

                if (_employee.SBU == "FB")
                {
                    BrandBudgetDAC dacBrandBudget = new BrandBudgetDAC(db);
                    List<BrandBudget> dataBrandBudget = dacBrandBudget.GetDataBrandFB();

                    PostBudgetDAC dacPostBudget = new PostBudgetDAC(db);
                    List<PostBudget> dataPostBudget = dacPostBudget.GetDataPostFB();

                    AreaDAC dacArea = new AreaDAC(db);
                    List<Area> dataArea = dacArea.GetData();

                    return Json(new { Employee = dataEmployee, BrandBudget = dataBrandBudget, PostBudget = dataPostBudget, Area = dataArea }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    BrandBudgetDAC dacBrandBudget = new BrandBudgetDAC(db);
                    List<BrandBudget> dataBrandBudget = dacBrandBudget.GetData(_employee.SBU);

                    PostBudgetDAC dacPostBudget = new PostBudgetDAC(db);
                    List<PostBudget> dataPostBudget = dacPostBudget.GetData(_employee.SBU);

                    AreaDAC dacArea = new AreaDAC(db);
                    List<Area> dataArea = dacArea.GetData();

                    return Json(new { Employee = dataEmployee, BrandBudget = dataBrandBudget, PostBudget = dataPostBudget, Area = dataArea }, JsonRequestBehavior.AllowGet);
                }


            }
            catch (Exception ex)
            {
                return Json(new { Error = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
1

1 Answers

1
votes

To reduce the payload, consider making 4 separate ajax calls, each to a different method that returns one of the 4 properties you need.

public ActionResult LoadInitEmployeeData()
{
  Employee dataEmployee =  ....
  ....
  return Json(dataEmployee, JsonRequestBehavior.AllowGet)
}

public ActionResult LoadBrandBudgetData()
{
  List<BrandBudget> dataBrandBudget = ....
  return Json(dataBrandBudget, JsonRequestBehavior.AllowGet)
}

etc.