Search code examples
ajaxasp.net-mvc-4razorwebgrid

asp.net mvc web grid is not showing pagination if there is only one record on the secound page


when a record is inserted from the front end of my website it populates the grid just after ajax form submission. i am using mvc web grid for showing data, the grid is restricted to show only 4 records per page.

if i insert the 5 record from the form it just disappears and the pagination does not show up until I put a 6th record in the grid,if done so the pagination shows up with 2 records on the second page what i don't understand is why is it now showing the 5th record when it is inserted and only show it after the 6 record is added.

here is controller action for grid

public ActionResult grid(int page = 1, int pageSize = 4, int AppointmentID = 0, int PatientID = 0)
    {

        AllergiesAppointmentList = ObjAllergiesRepository.PatientAllergiesAppointmentList(PatientID, AppointmentID, Helpers.SessionHelper._PracticeID, page, pageSize);

        int PatientIDForCheckOnly = 0;
        bool status = true;
        int callfor = 1;
        ViewBag.TotalAllergyCount = AllergiesAppointmentList.Count();
        //ViewBag.IsNKAStatus = ObjAllergiesRepository.PatientAllergyDetail_Profile(PatientIDForCheckOnly, status, );

        var recordAppointment = new CustomPaging<Allergies>();
        recordAppointment.Content = AllergiesAppointmentList.ToList();
        if (AllergiesAppointmentList.Count() != 0)
        {
            recordAppointment.TotalRecords = AllergiesAppointmentList.FirstOrDefault().TotalRecords;
        }
        else
        {
            recordAppointment.TotalRecords = 0;
        }
        recordAppointment.CurrentPage = 1;
        recordAppointment.PageSize = 5;

        return View(recordAppointment);
    }

Solution

  • In case any one else has the same problem. there are two points that can be used for the grid configuration. one is where you set the number of records yourself in the the controller and the query to the database returns records in that order. Second is on the web grid itself in the view. For this option you get all the records from the db and do pagination on the front end(Very expensive for large databases). for this setting you have to set the grid parameter like this

    var grid = new WebGrid(canPage: true, rowsPerPage: Your desired page size...
    

    What i was doing wrong was that i was asking my database to do pagination but also i was asking my web grid to do the same thing Hence wrong number of records was showing.