Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written Pagination code to display records on Ui.

Action Method:

C#
public ActionResult Questionnaire(int? page,FormCollection formcollection = null)
        {
            //Get the List of Questionnaires
            //problem, formCollection coming as null and the view is not returned 
            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

HTML
<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 .
Posted
Comments
F-ES Sitecore 7-Jan-20 4:44am    
What are you expecting formcollection to be? I'd start with consulting the documentation to see how the paging is supposed to be used.
chaturvedi_anshumaan_20191 7-Jan-20 6:34am    
FormCollection is suppose to capture and have the value selected in DropDownlist. For first page it is capturing. In second page i am doing a null check and it is getting passed.

@using (Html.BeginForm("Questionnaire", "ChurnFB", FormMethod.Post, new { @name = "ddlForm" }))
{
@Html.DropDownList("CLCode", new SelectList(ViewBag.CLCodeList, ViewBag.SelectedValue), "Select CLCode", new { id = "ddlCLCode" })
}

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