使用 JSON JavaScriptSerializer_Unable 序列化或反序列化期间出错,使用以前的答案进行修复

Error during serialization or deserialization using the JSON JavaScriptSerializer_Unable to fix using previous answers

我知道这是一个重复的问题,但我尝试了之前的所有答案。不为我工作。所以我在这里给出我的代码。当尝试加载超过 2000 条记录时出现此错误。

"Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property."

在控制器中

public ActionResult Index(SearchFilter sf)
{
Model m = new Model();
IList<Info> Details;
IList<rViewModel> RLst = new List<rViewModel>();
using (var db = new con())
 {
RLst = (from p in Details
      select new rViewModel
      {
      Id=p.Id,
      Description = p.Description,
      Remark = p.Remark,                               
   }).ToList();
   m.Result = RLst;
}
return View(m);
}

在视图中,

 $(document).ready(function () {
            $('#Product').dataTable({
                "columnDefs": [
                     { "width": "20%", "targets": 0, "orderable": true, },
                     { "width": "40%", "targets": 1,"orderable": true, },
                     { "width": "40%", "targets": 2,"orderable": true, },                     
                ],"oLanguage": {"sSearch": "Filter:"}
            });
            var t = $('#Product').DataTable();
            t.clear();t.draw();

            var model =@Html.Raw(Json.Encode(Model.Result));
            if(model!=null && model.length>0 )
            {
                AddData(model);
            }
            $('#Id').focus();
        });

Result Model其实就是分部视图。 在视图中,

var model =@Html.Raw(Json.Encode(Model.Result));

我收到这个错误。下面附上错误图片

如何修复?

我尝试添加

<system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="2147483644"/>
           </webServices>
       </scripting>
   </system.web.extensions> 

在 web.config 中已经 working.stuck 超过 2 天了.. 请帮助..

谢谢mr.Tetsuya山本的参考。到目前为止,我尝试了所有 link。但是根据您的 link 我尝试了下面的编码。

可见

@{
    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    serializer.MaxJsonLength = Int32.MaxValue;
    var jsonModel = serializer.Serialize(Model.Result);
}

在我出错的那一行,我改变了我的那个

var model =@Html.Raw(Json.Encode(Model.Result));

进入如下行:

var model =@Html.Raw(jsonModel);

之前我也尝试过这些编码,但是在控制器中输入错误。从你的 link 中我只发现需要放在视图中。这将帮助那些 web.config 声明不起作用的人。

Link 礼貌(再次)如下:Json Encode throwing exception json length exceeded

"jsonSerialization maxJsonLength" 这个问题对我没用

但我参考了@Halim 的回答。

这也适用于我:

之前有问题的代码:

<script>
    $(document).ready(function () {
        modelname(@Html.Raw(Json.Encode(Model)));
    });
</script>

代码解析:

<script>
    $(document).ready(function () {
        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        serializer.MaxJsonLength = Int32.MaxValue;
        var jsonModel = serializer.Serialize(Model);
        modelname(jsonModel)
    });
</script>