I have written Pagination code to display records on Ui.
Action Method:
public ActionResult Questionnaire(int? page,FormCollection formcollection = null)
{
var temp = formcollection;
questionnaires = new List<Questionnaire>();
questionnaires = GetQuestionnaire();
int pageIndex = 1;
int pageSize = 3;
pageIndex = page.HasValue ? Convert.ToInt32(page) : 1;
ViewBag.CLCodeList = questionnaires.Select(x => x.CLCode);
ViewBag.AnswerTypes = new List<string>() { "Number", "Text", "CheckBox" };
if (formcollection != null && formcollection["CLCode"]?.Length > 0)
{
ViewBag.SelectedValue = formcollection["CLCode"];
return View(GetQuestionnaire(formcollection["CLCode"].ToString()).ToPagedList(pageIndex, pageSize));
}
return View();
}
and the corresponding View.cshtml file
<div style="float:right;">
<br />
<label style="margin-top: -33px; margin-bottom: -16px;">Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount</label>
@Html.PagedListPager(Model, page => Url.Action("Questionnaire", new {page}))
</div>
When clicking on page 2 or any page number in my Ui, Formcollection is coming as null and then it is returning in empty view. I know for sure that Formcollection should not be null and if any records are ther then it should show on my UI. I have no idea why it is returning null and how can i display my next set of records in next page.
What I have tried:
I tried using ViewBag but it says method does ot exist in this context .