2

I want to display the records on details page when user click on next button then he should be able to display the next record of table. Suppose user select the details of a particular record id 1 he get the details of that id 1 at the same time on the same page by clicking the next button user should be able to get the record of id 2 and vice versa. I have done it but getting some error when table has no such id named id 3 after id 1 and id 2 its showing me the error. Please help me to find out where i am wrong.

View

@model WebApp.ViewModels.ViewTeamList
<script type="text/javascript">
   var dataid = '@Html.Raw(Json.Encode(Model.TeamDetails.TeamId))';
for( var item in dataid)
    console.log(dataid[item]);}();
</script>
<script type="text/javascript">
  $("#btnNext").click(function () {
    var $buttonClicked = $(this);
    var nid = $buttonClicked.attr('data-id');
    console.log(nid);
    $.ajax({
      url: 'Team/Next',
      data: { dataid: nid },
      //data: JSON.stringify(data.TeamId),
      success: function (response) {
       divDetail.html(data);
      }
    });
  });
</script>

<div class="row">
  <div class="col-md-11 col-sm-11 pull-left" style=" font-size:large; font-weight:600">
    @Model.TeamDetails.TeamName
  </div>
  @* <div class="col-md-1 col-sm-1 pull-right">*@
  <div class="navi-but">
    <a href="#" id="btnPrevious" data-id="@Model.TeamDetails.TeamId" class="details">
      <span class="previous">Previous</span>
    </a>
    <a href="#" class="details" data-id="@Model.TeamDetails.TeamId" id="btnNext">
      <span style="padding-right:7px">Next</span><span class="next"></span>
    </a>
    <li class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown">
        <img src="~/Images/settings.png" />
      </a>
      <ul class="dropdown-menu" role="menu">
        <li><a href="@Url.Action("Edit", "Team", new { id = Model.TeamDetails.TeamId })">Edit</a></li>
      </ul>
    </li>
  </div>
  @* </div>*@
</div>             
<div class="row">
  <div class="col-md-4 col-sm-4"> 
    @Html.CustomLabel("lblTeam","CT Team Name:")
  </div>
  <div class="col-md-8 col-sm-8">
    @Model.TeamDetails.TeamName
  </div>
</div>
<div class="row">
  <div class="col-md-4 col-sm-4"> 
    @Html.CustomLabel("lblDescription","Description:")
  </div>
  <div class="col-md-8 col-sm-8">
    @Model.TeamDetails.Description
  </div>
</div>
<div class="row">
  <div class="col-md-4 col-sm-4"> 
    @Html.CustomLabel("lblCTUserCount","User Count")
  </div>                         
  <div class="col-md-8 col-sm-8 pull-left">
    @Model.TeamDetails.UserCount
  </div>                  
</div>

Controller

public ActionResult Next(int dataid)
{
  dataid++;          
  ViewTeamList viewTeamList = new ViewTeamList();
  viewTeamList.ViewTeamDetails(dataid);
  return PartialView("_ViewTeamDetails", viewTeamList);
}

View model

public class ViewTeamList
{
  public TeamDetails TeamDetails;
  private ITeamService teamService;
  public ViewTeamList()
  {
  }
  public void ViewTeamDetails(int Id)
  {
    teamService = new TeamService(pDbContext);
    TeamDetails = teamService.GetTeamDetails(Id);
    //return (TeamDetails.First());
  }

}

Please help where i am doing wrong.

3
  • Your ajax call is replacing the whole <body> - hardly seems much point - you may as well just have an action link that passes the current ID to a method, and return a new view based on the next available ID. And putting that logic in a view model is awful practice - that belongs in the controller. Commented Dec 18, 2014 at 0:29
  • how can i find the next available id of a table sir @Stephen Muecke Commented Dec 18, 2014 at 10:30
  • Something like var next = db.TeamDetails.OrderBy(t => t.ID).Where(t => t.ID > CurrentID).FirstOrDefault(); Commented Dec 18, 2014 at 10:38

2 Answers 2

3

I didn't look your code in detail but it seems to me that you have a logical problem. Since you are always incrementing id by one ( dataid++; ) that won't work if some record is deleted in the meantime. For example let's say that you have Record1 with id 1, Record2 with id 2 and Record 3 with id 3 and you delete Record2. Now when you are trying to get next record after Record1 you are incrementing id by 1 so you have 2 and there is no record with id 2 in the db anymore. Instead of dataid++; you should find next id that really exists in db. As I said I didn't read code in detail so there may be more possible problems.

Sign up to request clarification or add additional context in comments.

3 Comments

exactly the same problem is happening. is there any alternative way ??
Of course. Like I said "Instead of dataid++; you should find next id that really exists in db." You can do it in many ways but here is one: 1) get all db records in list 2) sort the list by id 3) var indexOfCurrent = list.IndexOfKey(id); var next= list.Values[index+1];
There was one error in previous comment. It should go "var next= list[index+1];" instead of "var next= list.Values[index+1];". For previous you just take list element at index-1
0
  • To Display from WebMethod You Should follow these steps: create

    [webmethod]

to retrieve all the data

  • List items then make a javascript method in client side use:

    ajax({ type:'post', url:'exaple.aspx/showMethod', data:{}, datatype:'json', contentType:'application/json; charset=utf-8', scuccess:function(data) --display from here in table or any other data ), error:function(){ alert('Error'); } })


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.