Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC中的LargeJsonResult返回许多值_C#_Asp.net_Asp.net Mvc_Json_Asp.net Mvc 4 - Fatal编程技术网

C# MVC中的LargeJsonResult返回许多值

C# MVC中的LargeJsonResult返回许多值,c#,asp.net,asp.net-mvc,json,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Json,Asp.net Mvc 4,我有一个返回许多数据的控制器。然后我得到了“使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过了maxJsonLength属性上设置的值。” 我已经用这个添加了我的web.config。但错误仍然存在 <system.web.extensions> <scripting> <webServices> <jsonSerialization maxJsonLength="

我有一个返回许多数据的控制器。然后我得到了“使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过了maxJsonLength属性上设置的值。”

我已经用这个添加了我的web.config。但错误仍然存在

  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="2147483645" recursionLimit="100">
        </jsonSerialization>
      </webServices>
    </scripting>
  </system.web.extensions>
但是我如何将其用于许多返回数据呢?下面是我的控制器

return new LargeJsonResult() { Data = output, MaxJsonLength = int.MaxValue };
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);
            }
        }
public ActionResult LoadInitData()
{
尝试
{
数据库db=新数据库(“CON001”);
_employee=Helper.Common.GetEmployeeData(db);
EmployeeDAC dacEmployee=新员工edAc(db);
Employee dataEmployee=dacEmployee.GetDataByComputerLogin(GetUser());
如果(_employee.SBU==“FB”)
{
BrandBudgetDAC BrandBudget=新的BrandBudgetDAC(db);
List dataBrandBudget=dacbrandpubdget.GetDataBrandFB();
PostBudgetDAC PostBudget=新的PostBudgetDAC(db);
List datapostbrudge=dacpostbrudge.GetDataPostFB();
AreaDAC dac area=新的AreaDAC(db);
List dataArea=dacArea.GetData();
返回Json(新的{Employee=dataEmployee,BrandBudget=DataRandBudget,PostBudget=dataPostBudget,Area=dataArea},JsonRequestBehavior.AllowGet);
}
其他的
{
BrandBudgetDAC BrandBudget=新的BrandBudgetDAC(db);
List dataBrandBudget=dacbrandpubdget.GetData(_employee.SBU);
PostBudgetDAC PostBudget=新的PostBudgetDAC(db);
List datapostbrudge=dacpostbrudge.GetData(_employee.SBU);
AreaDAC dac area=新的AreaDAC(db);
List dataArea=dacArea.GetData();
返回Json(新的{Employee=dataEmployee,BrandBudget=DataRandBudget,PostBudget=dataPostBudget,Area=dataArea},JsonRequestBehavior.AllowGet);
}
}
捕获(例外情况除外)
{
返回Json(新的{Error=ex.Message},JsonRequestBehavior.AllowGet);
}
}

< /代码> 为了减少有效载荷,考虑制作4个单独的Ajax调用,每个调用一个返回需要的4个属性中的一个的方法。
public ActionResult LoadInitEmployeeData()
{
  Employee dataEmployee =  ....
  ....
  return Json(dataEmployee, JsonRequestBehavior.AllowGet)
}

public ActionResult LoadBrandBudgetData()
{
  List<BrandBudget> dataBrandBudget = ....
  return Json(dataBrandBudget, JsonRequestBehavior.AllowGet)
}
public ActionResult LoadInitEmployeeData()
{
雇员数据雇员=。。。。
....
返回Json(dataEmployee,JsonRequestBehavior.AllowGet)
}
公共操作结果LoadBrandBudgetData()
{
列表数据预算=。。。。
返回Json(dataBrandBudget、JsonRequestBehavior.AllowGet)
}

等等。

有没有办法不修改ajax?因为我在许多模块中调用此返回值首先检查它,您甚至不需要修改脚本(您没有发布任何内容,所以我不能确定)嗨,stephen,我可以通过在控制器上逐个声明返回值来完成。但似乎这类LargeJsonResult在我的情况下不起作用,我不知道如何解决这个问题。您可以考虑将4个单独的Ajax调用分成4个单独的方法,每个方法只返回其中一个属性。而且有效载荷会更小。嗨,斯蒂芬,非常感谢你的建议,我很喜欢你说的。我分离了ajax,它工作得很好。非常感谢:)是的,当然