我遇到了以下问题,呃尝试了一切,但我无法删除此错误并将数据列表到引用的表中。 这是我的模型项目:
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; }
}}
另一种是订货项目型号:....................................... ...........................
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; }
}}
这是控制器的一部分:...................................... ............
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);
}
错误是:
序列化“System.Data.Entity.DynamicProxies.PedidoItems_5E36688B06490D0B390FC180720173FC760260F84829E31D11D9F197ED76C978”类型的对象时检测到循环引用。
答案 0 :(得分:0)
解决方案是在控制器中添加此行:
db.Configuration.ProxyCreationEnabled = false;
离开这条道路:
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);
}
}