0

I'm trying to use the datatable in Jquery so I have a Controller with this code:

namespace wb01.Controllers
{
    public class ConsommationsController : Controller
    {
        private database db = new database();

        // GET: Consommations
        public ActionResult Index()
        {
            var consommation = db.Consommation.Include(c => c.Unités_de_prod);

            return View();
        }

        public ActionResult GetList()
        {

            using (database db = new database())
            {
            var listconsom = db.Consommation.ToList<Consommation>();
            return Json(new { data = listconsom }, JsonRequestBehavior.AllowGet);
            }
        }

and in my View I have this code: and in my View I have this code: and in my View I have this code: and in my View I have this code:

@model IEnumerable<wb01.Models.Consommation>

<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/css/jquery.dataTables_themeroller.css">
<br />
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table @*class="display"*@ id="tableconsommation">
    <thead>
        <tr>
            <th>Date_consommation</th>
            <th>Qtité_consommée</th>
            <th>Id_unité</th>
            <th>Unités_de_prod</th>
        </tr>
    </thead>
 <tfoot>
        <tr>
            <th>Date_consommation</th>
            <th>Qtité_consommée</th>
            <th>Id_unité</th>
            <th>Unités_de_prod</th>
        </tr>
    </tfoot>
</table>


@section scripts {
    <script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/jquery.dataTables.min.js"></script>

    <script>
        $(document).ready(function () {
            $("#tableconsommation").dataTable({
                "ajax": {
                    "url": "/Consommations/GetList",
                    "type": "GET",
                    "datatype": "json" 
                },
                "columns": [
                    { "data": "Date_consommation", "data": "Date_consommation" },
                    { "data": "Qtité_consommée", "data": "Qtité_consommée" },
                    { "data": "Id_unité", "data": "Id_unité" },
                    { "data": "Unités_de_prod", "data": "Unités_de_prod" },
                ],
                "serverSide": "true",
                "order": [0, "asc"],
                "processing": "true",
               });
        });
    </script>
}

but when I run my app, it shows this: enter image description here Or in my database I have more than 5 rows !, someone can help please?

lot
  • 39
  • 7
  • Is [this](https://datatables.net/) the jquery plugin your using? (if so it expects the data as an array of arrays (you generating and array of objects) –  Mar 20 '18 at 22:52
  • Yes it's a Jquery pulgin, and now Elmah is showing this error:"The ObjectContext instance has been disposed and can no longer be used for operations that require a connection" a probem with Json, How to fix that? Thanks – lot Mar 21 '18 at 21:13
  • Do not call the db in a `using` statement, or if you do, then add `.ToList()` to the query –  Mar 21 '18 at 21:15
  • I deleted using but I get an other error : circular references when serielizing an object "System.Data.Entity.DynamicProxies.Creux_silos" Creux_silos is an other table in my DB. – lot Mar 21 '18 at 21:37
  • The that means your `Consommation` model contains a property which is a model that contains a reference back to `Consommation`. All your view requires is 4 properties of your model, so only send those 4 properties (i.e. using a view model, or just an anonymous object) –  Mar 21 '18 at 21:39
  • I changed my Json and it 's working now but I get an other problem in the footer of the table shows: "Showing 0 to 0 of entries " ! and the fomat of date is strange like this (/Date(1520377200000)/). – lot Mar 22 '18 at 15:58
  • this my new code:` [HttpPost] public ActionResult GetList() { var list = db.Consommation.Select(x => new { Date_consommation = x.Date_consommation, Qtité_consommée = x.Qtité_consommée, Id_unité = x.Id_unité, Id_consommation = x.Id_consommation, Unités_de_prod = x.Unités_de_prod.Commentaires, }); return Json(new { data = list }, JsonRequestBehavior.AllowGet); }` – lot Mar 22 '18 at 16:01
  • You really need to start doing some research - [ASP.NET MVC JsonResult Date Format](https://stackoverflow.com/questions/726334/asp-net-mvc-jsonresult-date-format) –  Mar 22 '18 at 20:16
  • Thank you very much @Stephen, yes I researched a long time before but I didn't find my request, for the format I resolved it but the problem of table footer not yet. – lot Mar 23 '18 at 15:21

0 Answers0