-1

I am using following code in .cshtml file.

var typeVM = JSON.parse('@Html.Raw(Json.Encode(Model.typeVM))');

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

I dnt get any way to define max length for Json Encode function and Web.config configuration is only working for web-services.

So question is what is possible solution for this problem?
How do I define jsonMaxLength property?

Sabith
  • 1,628
  • 2
  • 20
  • 38
Sameer
  • 131
  • 1
  • 9
  • First, it should be `var typeVM = @Html.Raw(Json.Encode(Model.typeVM))` (its pointless to convert your json to a string and then convert it back again). The real question to ask yourself is why you are sending that much data to the view in the first place –  Apr 02 '18 at 10:59
  • Point is i got issue with escape sequence while directly convert string into json object. – Sameer Apr 03 '18 at 04:29

2 Answers2

1

I assume it is a web service that you are getting the data from (as your question is tagged "web-service"), change maxlength in web.config :

 <configuration> 
   <system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="50000000"/>
           </webServices>
       </scripting>
   </system.web.extensions>
</configuration> 
Shahzad Khan
  • 432
  • 2
  • 14
  • Thanks for help but its not web service . I tried it already its not working with MVC – Sameer Apr 03 '18 at 05:03
  • @Sameer that gonna work for mvc as well, try to add that configuration in your web.config file and let know if it's working or not. – Shahzad Khan Apr 03 '18 at 05:05
  • Actually, what's happens sometimes. In the model have large property name or model property with Underscore that way the json conversation is not working, please modify your model do it as you can and then again try the above statement which you have highlighted to us. thank you. – Shahzad Khan Apr 03 '18 at 05:09
  • Is there any way to define maxjsonlength property for whole project? – Sameer Apr 03 '18 at 05:22
  • 1
    There are three ways to define the maxjsonlength 1-in IIS, 2-Web.config and 3- return new JsonResult() { Data = data, JsonRequestBehavior = behavior, MaxJsonLength = Int32.MaxValue }; – Shahzad Khan Apr 03 '18 at 05:26
  • But your's scenario is different. you need to define the maxjsonlength in the .cshtml file. – Shahzad Khan Apr 03 '18 at 05:27
  • try to do this: var serializer = new JavaScriptSerializer(); serializer.MaxJsonLength = Int32.MaxValue; var resultData = new { Value = "Key", Text = "Test" }; var result = serializer.Serialize(resultData); – Shahzad Khan Apr 03 '18 at 05:33
  • Yea its working with server side but its not working in view. – Sameer Apr 03 '18 at 05:47
  • It's should be work in the view as well, please add these line in your view and hopefully it's gonna work. :) – Shahzad Khan Apr 03 '18 at 05:49
0
Model variable must be short 
for example 
model.First_name it should be short to model.FN

I hope that it will do work for you..

Md Nazrul Islam
  • 363
  • 4
  • 13