Hi, again this is Part 2 from DataTables ASP.NET MVC 2.

In Part 1, I showed you how to add all data required.

Now let's try to finish what we start together.

Now will try to add new Controller and Call it StudentController.

 

The most important one is, what I highlight it (Yellow).

First one is declared our Interface,

  private readonly IStudent repositoryStudent;

building contractor.

 public StudentController(IStudent repoStudent)
        {
            repositoryStudent = repoStudent;
        }

 

This one our main job :)

  public ActionResult DataTablesJQueryJSON()
        {

         } 

ActionRsult will return JSON data to HTML page.

 [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public ActionResult DataTablesJQueryJSON()
        {
            List<Student> model = new List<Student>();
            model = repositoryStudent.GetStudent; 
            var UserData = new object[model.Count];
            int j = 0;
            foreach (var i in model)
            {
                UserData[j] = new object[] {i.ID, i.FirstName ,i.LastName, i.City};
                j++;
            }
           
            return Json(UserData, JsonRequestBehavior.AllowGet);
        }

 

In last ActionResult, we read data from StudentRepository.

Store it in a model variable.

UserData to store our data after convert it to Object (ordering Important).

Create New View Call it DataTablesJQuery.

After adding HTML Table show be same what ActionResult will return,

I mention it before, ordering is important. 

The Big Boss our JavaScript code with AJAX will call DataTablesJQueryJSON method.

All that will be send to the #example HTML table.

To be Shown like this. 

 

Look at the previous Photo,

look, how many free features you add with simple code. 

 

Source Code

Youtube Videos

if you have any question,

Please, write it in the comment.



Share

Comments