1

I'm trying to send the queried data gathered from the context back to the ajax caller with the following status

1-The source code server side succeeds querying the data. 2-The source code server side succeeds sending the data in case HTTPPOST or HTTPGET is used 3-No exceptions or errors at the source code 4-applying JSON(queried object) method doesn't make a change The scenario goes as follow:- using ajax call as follows

$.ajax({
                    type: "POST",
                    url: '@Url.Action("MainCatItems", "Cachier")',
                    contentType: "application/json; charset=utf-8",
                    data: datas,
                    dataType: "json",
                    success: successFunc,
                    error: function (xhr) {
                        alert('Error: ' + xhr.statusText);
                    },
                });

calling an Action Controller

[HttpPost]
    public ActionResult MainCatItems(int mainCatNo)
    {
        var results = ......//fetching data from Context
        return Json(new { data = results }, JsonRequestBehavior.AllowGet);
    }

, the call succeeds sending my parameters to the action but sending a response back to the ajax call from the controller is a little bit confusing as I have two cases:

The First: (working perfectly) I'm returning data queried from a table class in my Entity Framework (RegisterNotification Class)

public partial class RegisterNotification
{
    //setters and getters
}

which is a single class with no navigational property declared inside

The Second(where the problem is.) I'm returning data queried from a table class in my Entity Framework (Item Class"partial class")

Part One:-

public *partial* class Item
{
    // setters and getters
}

Part Two:-

 public *partial* class Item
{
    //setters and getters
}

which is a partial class with navigational properties declared inside

This case doesn't work and when getting back to the ajax call it alerts the error function message saying an "internal server error 500" I have tried parsing the queried data to a JSON Object in my controller:-

var t = Json(res,"application/json; charset=utf-8",JsonRequestBehavior.AllowGet);

Queried Data at source code server side Watch:

Client Side error message details

But again with failure. Thanks for concerning.

  • "internal server error 500" is not enough to provide any useful answer. Please make sure to debug the code and update post with exact original exception and partial stack trace. Cleaning up sample to be close to [MCVE] would be welcome too. – Alexei Levenkov Apr 01 '17 at 02:45
  • what is this `_model.Item`? it's still under the cover. There must be some exception achieving the data, you should set some break point to see if the data is actually fetched OK before JSONizing it and returning to client. – King King Apr 01 '17 at 02:47
  • @KingKing _model is the instance created from my Entity Framework Context it contains all the classes mapped from the DataBase including Item Class – Alhussien S Madian Apr 01 '17 at 02:53
  • @AlexeiLevenkov Ok your note is considered I'm editing the post now trying to make it minimal as possible but for the error clarification this is the best details I could get which is internal server error when using the inspection tool in google chrome any recommendation I would gratefully thankful – Alhussien S Madian Apr 01 '17 at 02:56
  • @AlhussienSMadian it looks internal ***to the clients***, but should not to you. You have the source code, you can debug it, can't you? – King King Apr 01 '17 at 02:57
  • You need to ask author/owner of C# (server side) code to debug it and provide info. There is really nothing browser debugging tools can help you with here. – Alexei Levenkov Apr 01 '17 at 02:58
  • @KingKing and as for the data, it is fetched perfectly fine and I can watch them with all of their details. is there any limitation for using partial class than using only a class in returning data to the ajax caller ? – Alhussien S Madian Apr 01 '17 at 03:00
  • @AlexeiLevenkov I am the owner and I have privilege debugging the server-side and it seems perfect, I mean the data is fetched and turned into list of IENUMERABLE and returned to the ajax caller but directed to the error function – Alhussien S Madian Apr 01 '17 at 03:05
  • @AlhussienSMadian can you show how are you getting data from context? – Usman Apr 01 '17 at 03:42

2 Answers2

1

set ProxyCreationEnabled to false like this

[HttpPost]
public ActionResult MainCatItems(int mainCatNo)
{
    entity.Configuration.ProxyCreationEnabled = false;
    var results = _model.Item.Where(x => x.MenuCategoryID == mainCatNo).ToList();
    return Json(new { data = results }, JsonRequestBehavior.AllowGet);
}

you are using entity framework generated proxy object which might be causing problems in serializing

Usman
  • 4,615
  • 2
  • 17
  • 33
  • 1
    WOW, That solved the problem, so the problem is caused due to the navigational property? It seems that I need to learn more about Serializing /Deserializing. Thank you very much, I tried to upvote but it seems I don't have the privilege doing that. – Alhussien S Madian Apr 01 '17 at 04:02
  • its ok i am glad that it helped :) – Usman Apr 01 '17 at 04:03
  • 1
    `ProxyCreationEnabled` relates to dynamic type (created at runtime by the engine), for strict serializing/deserializing, the type info should be known to the client (to recreate the data). However for JSON, it would be strange to have that requirement. Technically, JSON would work on any kind of object and its interface is just platform-independent. Once being serialized, you don't need to know about its source/type anymore. The client determines itself how to deserialize the data back. Looks like this is some kind of strict design we must follow. – King King Apr 01 '17 at 04:13
0
public JsonResult GetCatItems(int mainCatNo)
    {
        var results = ......//fetching data from Context
        return Json(new { data = results }, JsonRequestBehavior.AllowGet);
    }

Try This .. It might Help You And you can call this method from an action controller.

$.ajax({
                    type: "POST",
                    url: '@Url.Action("GetCatItems", "Cachier")',
                    contentType: "application/json; charset=utf-8",
                    data: datas,
                    dataType: "json",
                    success: successFunc,
                    error: function (xhr) {
                        alert('Error: ' + xhr.statusText);
                    },
                });
Dashanan
  • 122
  • 3
  • 16
  • Searching for the differences between returning (Action/JSON)Result :[link](http://stackoverflow.com/questions/26390806/when-to-use-jsonresult-over-actionresult), I understood that both JsonResult and ActionResult override ExecuteResult method, and that if we use ActionResult in returning data to the ajax caller at the client side, client will have to cast that to the required JSON object But in our case, we already casting it to the required JSON object using this line: (return Json(new { results }, JsonRequestBehavior.AllowGet);) hence we have the same problem (Proxy usage) – Alhussien S Madian Apr 01 '17 at 11:51