0

Hey I'm trying to receive data with Ajax in ASP.NET MVC, but this code doesn't seem to be working.

return this.Json(new
        {
            Result = (from obj in temp
                      select new
                      {
                          ObjektoAdresas = obj.ObjektoAdresas,
                          ĮrenginioNumeris = obj.ĮrenginioNumeris,
                          T1 = serializer.Serialize(obj.T1),
                          T2 = serializer.Serialize(obj.T2),
                          ŠiluminėEnergija = serializer.Serialize(obj.ŠiluminėEnergija),
                          ŠiluminėGalia = serializer.Serialize(obj.ŠiluminėGalia)
                      })
        }, JsonRequestBehavior.AllowGet);

This throws a NotSupportedException with additional information: Only parameterless constructors and initializers are supported in LINQ to Entities.

As I understand I can't use serializer.Serialize(something) because it takes a parameter. Is that correct? And if so, how do I serialize this data to JSON before sending it?

EDIT: I changed the code after Will's response, but it still throws the same exception.

var temp = (from obj
                    in models
                    select obj).ToArray();

        return this.Json(new
        {
            Result = (from obj in temp
                      select new
                      {
                          ObjektoAdresas = obj.ObjektoAdresas,
                          ĮrenginioNumeris = obj.ĮrenginioNumeris,
                          T1 = serializer.Serialize(obj.T1),
                          T2 = serializer.Serialize(obj.T2),
                          //Srautas = serializer.Serialize(obj.Srautas),
                          //VandensKiekis = serializer.Serialize(obj.VandensKiekis),
                          ŠiluminėEnergija = serializer.Serialize(obj.ŠiluminėEnergija),
                          ŠiluminėGalia = serializer.Serialize(obj.ŠiluminėGalia)
                      })
        }, JsonRequestBehavior.AllowGet);
Kipras
  • 163
  • 7
  • I think I misread things... Go through ALL your EF models and make sure none of them have constructors. –  Jul 17 '15 at 14:23
  • It throws when you call `ToArray`, correct? Catch the exception, call ToString on it, and paste the result in an [edit], pls. –  Jul 17 '15 at 14:46
  • Why are you manually serializing the contained classes `T1`, `T2`, and so on? When you do `return this.Json(...)` your object hierarchy gets serialized for you by the returned [`JsonResult`](https://msdn.microsoft.com/en-us/library/system.web.mvc.jsonresult%28v=vs.118%29.aspx), using [`JavaScriptSerializer`](https://stackoverflow.com/questions/14591750/setting-the-default-json-serializer-in-asp-net-mvc). – dbc Jul 17 '15 at 17:41
  • But if I don't manually serialize the objects than the ajax call fails. – Kipras Jul 20 '15 at 05:41
  • Sorry, but the problem wasn't in this piece of code, but rather in another place. I was selecting new KeyValuePairs and using the constructor to initialize them, so I changed that and now it works. Thanks for trying to help. – Kipras Jul 20 '15 at 11:25

0 Answers0