2013-02-01 74 views
0

我正在ASP.Net Mvc3中開發Web調查應用程序。我在我的應用程序中使用PagedList來單獨分頁問題頁面。使用PagedList時的模型項目類型歧義

我得到以下錯誤:

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]'. 

有一個question這是類似性質。就我所知,解決方案表示不指定匿名類型。有人能指出我的代碼中有匿名類型嗎?我相信我已經用適當的模型輸入了所有的變量。

這是問題控制器:

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)); 
    } 

有兩種模式:

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>(); 
    } 
} 

我進入我的兼職索引視圖代碼上述問題控制器

@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) 
     } 

我也有這樣的編輯模板

@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> 

我試圖通過代碼運行幾次,很難搞清楚它。我也不知道如何將questionList作爲LINQ變量,因爲我的questionList是由單獨模型中的問題和選擇構建的。

回答

1

PagedList的創建者在這裏。問題是,這條線:

return View(questionList.Questions.ToPagedList(pageNumber, pageSize)); 

派遣型IPagedList模型下到頁面(因爲擴展方法被應用到列表類型),但您的網頁說,這是期待:

@model PagedList.IPagedList<SWSSMVC.Models.ViewModels.QuestionListViewModel> 

更改您的視圖代碼說,這不是應該修復它:

@model PagedList.IPagedList<SWSSMVC.Models.ViewModels.QuestionViewModel> 
+0

感謝那些工作。由於集合現在是IPagedList,我的編輯器模板不起作用。我必須找出一個辦法來做到這一點.. –

相關問題