0

I have an EntityFramework query in C # in an MVC, the problem is that the result of my code returns undefined, the code in C #:

public ActionResult ListarProductos()
{
    sistemaEntities context = new sistemaEntities();
    var lista = (from p in context.productos
             select p).Where(p => p.nombre_producto.Contains(""));
    return Json(new { success = true, message = lista }); // RETURN UNDEFINED
    //return Json(new { success = true, message = "hola" }); // RETURN VALID OBJECT
}

The code in Ajax:

app.factory("AppService", function() {
  return {
    listProductos: function() {
        var idata;
        $.ajax({
          type: 'POST',
          url: "/CRUD/ListarProductos",
          data: {},
          dataType: 'json',
          async: false,
          success: function(result){idata = result;}
        });
        alert(idata);
        return idata;
    }, .. more code

The result in Ajax is "undefined" and I can not find a solution, the result in Ajax I then use it in AngularJS to display a table.

Not a duplicate, the second line of the code in c# works fine in ajax, the problem is the list in entity framework

The test line is commented out and returns a valid object

How can i fix this ?

NemoBlack
  • 143
  • 1
  • 11
  • 2
    Put your alert inside success callback so that it will alter when the result is recevied from server, not before that! – Shyju Jan 12 '17 at 19:58
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – David Jan 12 '17 at 19:59
  • Not a duplicate, the second line of the code in c# works fine in ajax, the problem is the list in entity framework – NemoBlack Jan 12 '17 at 20:00
  • The test line is commented out and returns a valid object – NemoBlack Jan 12 '17 at 20:01
  • 1
    Are you saying your `alert(idata)` displays undefined, or that inside your `success` function, `result` has a `success` property assigned `true`, and a `message` property assigned `undefined`? – krillgar Jan 12 '17 at 20:02
  • @NemoBlack Could you return `return Json(new { success = true, message = new List() });` and see it work? – Win Jan 12 '17 at 20:10
  • yes it work , why my code fails ? – NemoBlack Jan 12 '17 at 20:17

2 Answers2

0

Please, try to change the line: var lista = (from p in context.productos select p).Where(p => p.nombre_producto.Contains(""));

to: var lista = (from p in context.productos select p).Where(p => p.nombre_producto.Contains("")).ToList();

I assume that "lista" is an list of EF proxy classes and the JSON serializer cannot serialize it.

Ionut Ungureanu
  • 1,020
  • 1
  • 13
  • 16
0

Based on comment - return Json(new { success = true, message = new List<productos>() }); works.

It might be that productos object has circular references.

You can disable ProxyCreationEnabled, and try again.

context.Configuration.ProxyCreationEnabled = false;

Or return anonymous object with just properties that you need.

var lista = (from p in context.productos
   where p.nombre_producto.Contains("")
   select new { nombre_producto = p.nombre_producto }).ToList();
Win
  • 61,100
  • 13
  • 102
  • 181