【问题标题】:Model Item type ambiguity when using PagedList使用 PagedList 时模型项类型不明确
【发布时间】:2013-02-01 05:19:06
【问题描述】:

我正在用 ASP.Net Mvc3 开发一个网络调查应用程序。我在我的应用程序中使用 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 又是由来自单独模型的问题和选择构成的。

【问题讨论】:

    标签: asp.net asp.net-mvc-3 pagedlist


    【解决方案1】:

    PagedList 的创建者在这里。问题是这一行:

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

    正在将 IPagedList 类型的模型发送到页面(因为扩展方法正在应用于 List 类型),但您的页面显示它正在等待:

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

    将您的视图代码改为这样应该可以修复它:

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

    【讨论】:

    • 感谢工作。由于该集合现在是 IPagedList,因此我的编辑器模板不起作用。我必须想办法做到这一点..
    猜你喜欢
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多