Error The ObjectContext instance asp.net

0

Could you help me with this error asp.net The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. this is my code controller

 public ActionResult GetData()
        {

            using (SISTEMAUSGEntities db = new SISTEMAUSGEntities())
            {


                List<PROYECTOS> empList = db.PROYECTOS.ToList<PROYECTOS>();
                return Json(new { data = empList }, JsonRequestBehavior.AllowGet);
            }

        }

        [HttpGet]
        public ActionResult AddOrEdit(int id = 0)
        {
            if (id == 0)
                return View(new PROYECTOS());
            else
            {
                using (SISTEMAUSGEntities db = new SISTEMAUSGEntities())
                {
                    return View(db.PROYECTOS.Where(x => x.IDPROJECT == id).FirstOrDefault<PROYECTOS>());
                }
            }
        }

view

    <a class="btn btn-success" style="margin-bottom:10px" onclick="PopupForm('@Url.Action("AddOrEdit","Project")')"><i class="fa fa-plus"></i> Add New</a>
<table id="ProjectTable" class="table table-striped table-bordered" style="width:100%">
    <thead>
        <tr>
            <th>NAMEPROJECT</th>
            <th>NUMPART</th>
            <th>TIPOMATE</th>
            <th>PAIS</th>
            <th>NUMFACTURA</th>
            <th>PESOPALLET</th>
            <th>NUMPALLET</th>
            <th>FECHA</th>
            <th>STATUSPROJECT</th>
            <th></th>
        </tr>
    </thead>

</table>
<link href="https://cdn.datatables.net/1.10.15/css/dataTables.bootstrap.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />

@section scripts{
    <script src="//cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.15/js/dataTables.bootstrap.min.js"></script>

    <script>
        var Popup, dataTable;
        $(document).ready(function () {
            dataTable = $("#ProjectTable").DataTable({
                "ajax": {
                    "url": "/Project/GetData",
                    "type": "GET",
                    "datatype": "json"
                },
                "columns": [
                    { "data": "NAMEPROJECT" },
                    { "data": "NUMPART" },
                    { "data": "TIPOMATE" },
                    { "data": "PAIS" },
                    { "data": "NUMFACTURA" },
                    { "data": "PESOPALLET" },
                    { "data": "NUMPALLET" },
                    { "data": "FECHA" },
                    { "data": "STATUSPROJECT" },
                    { "data": "IDPROJECT" , "render" : function (data) {
                        return "<a class='btn btn-default btn-sm' onclick=PopupForm('@Url.Action("AddOrEdit","Project")/" + data + "')><i class='fa fa-pencil'></i> Edit</a><a class='btn btn-danger btn-sm' style='margin-left:5px' onclick=Delete("+data+")><i class='fa fa-trash'></i> Delete</a>";
                    },
                        "orderable": false,
                        "searchable": false,
                        "width": "150px"
                    }
                ],
                "language": {

                    "emptyTable": "No data found, Please click on <b>Add New</b> Button"
                }
            });
        });
    
asked by Daniel 11.12.2017 в 23:18
source

2 answers

0

Solution, just add this line of code

                db.Configuration.ProxyCreationEnabled = false;

stayed like this

  public ActionResult GetData()
    {
        using (SISTEMAUSGEntities db = new SISTEMAUSGEntities())
        {
            db.Configuration.ProxyCreationEnabled = false;
            List<PROYECTOS> empList = db.PROYECTOS.ToList<PROYECTOS>();
            return Json(new { data = empList }, JsonRequestBehavior.AllowGet);
        }

    }
    
answered by 12.12.2017 / 17:52
source
0

modify your code in the following way,

    public ActionResult GetData()
    {
        List<PROYECTOS> empList;
        using (SISTEMAUSGEntities db = new SISTEMAUSGEntities())
        {
            empList = db.PROYECTOS.ToList<PROYECTOS>();

        }
        return Json(new { data = empList }, JsonRequestBehavior.AllowGet);
    }
    
answered by 12.12.2017 в 07:31