Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working on the following :-

  • asp.net mvc5 web application.
  • dataTable jQuery plugins version 1.10.4

but i have noted that i can not benefit from these asp.net mvc features , if i use the dataTable plugin:-

  • I can not use html helpers.

  • The data annotation applied will have no effect.

For example I have the following Script to implement the dataTable plugin:-

<script type="text/javascript">
        $(document).ready(function () {

            $('#myDataTable').dataTable({
                "serverSide": true,
                "ajax": "Home/AjaxHandler",
                "processing": true,
                "columns": [
                            { "data": "FName" },
                            { "data": "LName" },
                            { "data": "BDate" }
                ]
            });
        });
    </script>

And the following action method:-

 public ActionResult AjaxHandler(jQueryDataTableParamModel param)
            {
                var allCompanies = t.Emps;

                var result = allCompanies.Select(c=> new {c.FName, c.LName, c.BDate});
                           //  select new[] {  };

                return Json(new
                {
                    sEcho = param.sEcho,
                    iTotalRecords = allCompanies.Count(),
                    iTotalDisplayRecords = allCompanies.Count(),
                    aaData = result
                },
                                JsonRequestBehavior.AllowGet);
            }

And the following view:-

@{
    ViewBag.Title = "Home Page";
}





<table id="myDataTable" class="display">
    <thead>
        <tr>
            <th>FName</th>
            <th>LName</th>
            <th>BDate</th>

        </tr>
    </thead>
    <tbody></tbody>
</table> 

And the following model:-

namespace testplugins.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class Emp
    {
        public int EmpID { get; set; }
       [Display(Name = "First Name")]
        public string FName { get; set; }
        [Display(Name = "Last Name")]
        public string LName { get; set; }
        public Nullable<int> DeptID { get; set; }
        [DataType(DataType.Date)]
        public Nullable<System.DateTime> BDate { get; set; }

        public virtual Dept Dept { get; set; }
    }
}

Now for example I can not use Html.DisplayNameFor(a=>a.FName) and get the name as defined inside the data annotation. and instead of that i need to manually write the html table headers labels.

Also for the Bdate the value inside the dataTable will be displayed as “/Date(315536524000)/” so I even can not benefit from DataType.Date data annotation, also if I am going to edit the records using the dataTable plugin then data annotation such as Required, stringlenght will also have no effect.

So can anyone advice on this please? and can i modify the dataTable plugin to be more asp.net mvc oriented ? or i am doing things wrongly in my above code ?

share|improve this question
    
you can follow the approach shown here –  Aminul 16 hours ago
    
@Aminul i already read this link and it basically caused me to write this question, after i test it. because as you see from the example there is no way to use html helpers or benefit from the data annotations... –  john G 15 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.