Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Pagination using AJAX(I made a PartialView so page is not going to refresh every time when yser click Next button to view next record)
While retrieving Data from 1 table it works perfectly fine,but it throws error while fetching data from 2 tables using navigational properties when click Next button :" An error occurred while executing the command definition "

Anyone has any idea where im doing wrong?

What I have tried:

Controller :

I wrote this code in Index() ------ This is HttpGet
var tsk = db.Tasks.Include(e => e.Customers);
 ViewBag.CurrentPage = 1;
 ViewBag.LastPage = Math.Ceiling(Convert.ToDouble(tsk.ToList().Count)/5);
 return View(tsk.Take(5));


And for HttpPost Index ()
 ViewBag.CurrentPage = CurrentPage;
 ViewBag.LastPage = LastPage;
return PartialView("_Task", db.Tasks.OrderBy(v => v.TaskID).Skip((CurrentPage - 1) * 5).Take(5));


View :

<div style="width: 600px;">
      @Html.ActionLink("Add Task", "Create", "Home", null)
      <div id="TaskDiv">
          @Html.Partial("_Task", Model)
      </div>
  </div>
  <div>
      @using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "TaskDiv" }))
      {
          <input type="hidden" id="CurrentPage" name="CurrentPage" value="@ViewBag.CurrentPage" />
          <input type="hidden" id="LastPage" name="LastPage" value="@ViewBag.LastPage" />
      }
      <input type="button" id="Previous" value="Previous" />
      <input type="button" id="Next" value="Next" />
  </div>


I also wrote some JQuery and JS code

Partial View
<tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>@item.Customers.Name</td> // Using IEnumerable Task Model // Throw error here when clicking Next Button 
                <td>@item.Name</td>
                <td>@item.StartDate</td>
                <td>@item.EndDate</td>
                <td>Aagent Name</td>
                <td>Status</td>
            </tr>
        }
    </tbody>
Posted
Updated 10-Sep-16 15:17pm
v2
Comments
Patrice T 10-Sep-16 22:37pm    
And you have plan to tell the error message ? and where ?
tahirraza21 10-Sep-16 22:43pm    
As I mentioned above error occurs in Partial View when Next button clicked for the very first time . Error pointing at this code of line <td>@item.Customers.Name</td>
Patrice T 10-Sep-16 23:34pm    
And the error message is ?

Use Improve question to update your question.
So that everyone can pay attention to this information.
tahirraza21 10-Sep-16 23:45pm    
Can't find option " improve question" anyway this is the error which i have already mentioned in the question
"An error occurred while executing the command definition . See inner Exception "
And inner exception doesn't show any exception
Sreekanth Mothukuru 12-Sep-16 23:21pm    
I guess you need to check the @model that you are using for the partial view when retrieving from single table or from two tables.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900