I am developing a web survey application in ASP.Net Mvc3. I use PagedList in my application to paginate the questions page alone.
I get the following error:
The model item passed into the dictionary is of type
'PagedList.PagedList`1[SWSSMVC.Models.ViewModels.QuestionViewModel]',
but this dictionary requires a model item of type
'PagedList.IPagedList`1[SWSSMVC.Models.ViewModels.QuestionListViewModel]'.
There is a question which is of similar nature. The solution says not to specify anonymous type, as far as I understood. Can someone point out where in my code I have anonymous type? I believe I have typed all my variables with appropriate models.
This is the question Controller:
public class QuestionController : SessionController
{
DBManager dbmgr = new DBManager();
//
// GET: /Question/
public ActionResult Index(string currentSection, string currentPage, int? page)
{
int j;
SectionSession = currentSection;
PageSession = currentPage;
var questionList = new QuestionListViewModel();
int questionCount = dbmgr.getQuestionCount(currentPage);
var question = new QuestionViewModel();
for(int i=1 ; i<=questionCount; i++)
{
int questionid = dbmgr.getQuestionid(currentPage, i);
string questiontext = dbmgr.getQuestion(questionid);
List<string> oldchoices = dbmgr.getChoicesAns(questionid);
ChoiceViewModel choice = new ChoiceViewModel();
question = new QuestionViewModel { QuestionId = questionid, QuestionText = questiontext, Answer = oldchoices.Last()};
for (j = 0; j < oldchoices.Count() - 1; j++)
{
if (oldchoices[j] != null)
{
question.Choices.Add(new ChoiceViewModel { ChoiceId = j, ChoiceText = oldchoices[j] });
}
}
questionList.Questions.Add(question);
}
int pageSize = 3;
int pageNumber = (page ?? 1);
return View(questionList.Questions.ToPagedList(pageNumber, pageSize));
}
There are two models:
public class QuestionViewModel
{
public int QuestionId { get; set; }
public string QuestionText { get; set; }
public List<ChoiceViewModel> Choices { get; set; }
public string Answer { get; set; }
[Required]
public string SelectedAnswer { get; set; }
public QuestionViewModel()
{
Choices = new List<ChoiceViewModel>();
}
}
public class QuestionListViewModel
{
public List<QuestionViewModel> Questions { set; get; }
public QuestionListViewModel()
{
Questions = new List<QuestionViewModel>();
}
}
I am entering my part- Index View code for the above Question Controller
@model PagedList.IPagedList<SWSSMVC.Models.ViewModels.QuestionListViewModel>
@{
ViewBag.Title = "Index";
}
<h2>Questions</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
@foreach (var item in Model)
{
@Html.EditorFor(x => item.Questions)
}
I also have a Editor Template like this
@model SWSSMVC.Models.ViewModels.QuestionViewModel
<div>
@Html.HiddenFor(x => x.QuestionId)
<h3> @Model.QuestionText </h3>
@foreach (var a in Model.Choices)
{
<p>
@Html.RadioButtonFor(b => b.SelectedAnswer, a.ChoiceText) @a.ChoiceText
</p>
}
</div>
I tried to run through the code a couple of times and having hard time figuring it out. I also do not know how I could have made the questionList as a LINQ variable, given that, my questionList is inturn constructed with questions and choices from a separate model.
Creator of PagedList here. The problem is that this line:
Is sending a model of type IPagedList down to the page (because the extension method is being applied to a type of List), but your page says it is expecting:
Changing your view code to say this instead should fix it: