0

I'm having the following problem, eh tried everything but I can not remove this error and list data to the referenced table. This is my Model Items:

namespace Project.Models
 {
//[Bind(Exclude = "ID")]
public class Item
{
    private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

    [Key]
    //[ScaffoldColumn(false)]
    [DisplayName("ID Producto")]
    public int ProductoID { get; set; }

    // FOREIGN KEY
    [DisplayName("SubCategoria")]
    public int SubCategoriaID { get; set; }

    // FOREIGN KEY
    [DisplayName("Proveedor")]
    public int ProveedorID { get; set; }

    [Required(AllowEmptyStrings = false,ErrorMessage = "El codigo es obligatorio")]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    [DisplayName("Codigo")]
    public string Codigo { get; set; }        

    [Required(ErrorMessage = "El nombre obligatorio")]
    [StringLength(160)]
    [DisplayName("Nombre")]
    public string Nombre { get; set; }        

    public virtual SubCategoria SubCategoria { get; set; }

    public virtual List<PedidoItems> PedidoItems { get; set; }

    public virtual Proveedor Proveedor { get; set; }

    public List<Imagen> Imagenes { get; set; }

}}

The other is the model Order Items:..................................................................

namespace Project.Models
{
public class PedidoItems
{
    public int PedidoItemsId { get; set; }
    public int PedidoId { get; set; }
    public int ProductoID { get; set; }
    public int iCantidad {get;set;}
    public decimal dPrecio {get;set;}
    public decimal SubTotal { get; set; }

    public virtual Item Item { get; set; }
    public virtual Pedido Pedido { get; set; }

}}

And this is the part of the controller: ..................................................

public ActionResult MisPedidosDetalle([DataSourceRequest] DataSourceRequest request) { 
       var query = from p in db.PedidoItems //.Include("Items")
        select new
        {
            p.PedidoId,
            p.ProductoID,
            p.iCantidad,
            p.dPrecio,
            p.SubTotal,
            Item = p.Item
        };

    return Json(query.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);        
      }

And the error is:

A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.PedidoItems_5E36688B06490D0B390FC180720173FC760260F84829E31D11D9F197ED76C978'.

Max
  • 538
  • 1
  • 6
  • 16
  • You think may be due to that? I believe I should not have problems :( – Max Oct 04 '16 at 20:34
  • Ah, does [Circular reference detected exception while serializing object to JSON](http://stackoverflow.com/questions/16949520/circular-reference-detected-exception-while-serializing-object-to-json) have an answer for you? – Andrew Morton Oct 04 '16 at 20:40
  • Thank you very much! And I had read but now I understand. Simply add this line in the controller: db.Configuration.ProxyCreationEnabled = false; – Max Oct 04 '16 at 21:06

1 Answers1

0

The Solution is add this line in the controller:

db.Configuration.ProxyCreationEnabled = false;

leaving this way:

 public ActionResult MisPedidosDetalle(int PedidoId, [DataSourceRequest] DataSourceRequest request)
    {
        try
        {
            db.Configuration.ProxyCreationEnabled = false;
            var query = from p in db.PedidoItems //.Include("Items")
                        where p.PedidoId == PedidoId
                        select new
                        {
                            p.PedidoId,
                            p.ProductoID,
                            p.iCantidad,
                            p.dPrecio,
                            p.SubTotal,
                            Item = p.Item
                        };

            return Json(query.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            return Json(ex.Message);
        }


    }
Max
  • 538
  • 1
  • 6
  • 16