Asp.net mvc 4 无法在httpget方法中将模型从控制器传递到视图

Asp.net mvc 4 无法在httpget方法中将模型从控制器传递到视图,asp.net-mvc-4,Asp.net Mvc 4,我想做有问题的分页dta是我想每页显示一个问题 这是我的模型: public class Question { public int QuestionId { get; set; } public string QuestionName { get; set; } public List<QuestionOption> Options { get; set; } public int SelectedOptio

我想做有问题的分页dta是我想每页显示一个问题

这是我的模型:

 public class Question
    {
        public int QuestionId { get; set; }
        public string QuestionName { get; set; }
        public List<QuestionOption> Options { get; set; }
        public int SelectedOption { get; set; }
    }

    public class QuestionOption
    {
        public int OptionId { get; set; }
        public string OptionName { get; set; }
    }
这是我的控制器:

[HttpGet]
        public ActionResult TestStarted(int TestId)
        {
            int pg_cnt = 0;
            ViewBag.ct = 0;
            ViewBag.Test_id = TestId;

            List<Question> model = new List<Question>();
            model = new Test_Planning().Fetch_Question_By_Test(TestId);
            pg_cnt = model.Count();
            ViewBag.total = pg_cnt;
            if (Request.QueryString["cnt"] != null)
            {
                int count = Convert.ToInt16(Request.QueryString["cnt"].ToString());
                ViewBag.ct = count;
                return View(model.Skip(1 * count).Take(1));
            }
            else
            {
                return View(model.Skip(0).Take(1));
            }
        }
这是我的查看页面:

@model List<EAssessmentNew.Models.Question>

@{
    ViewBag.Title = "TestStarted";
    Layout = "~/Views/StudentMaster.cshtml";
}

@using (Html.BeginForm("TestStarted","Student",FormMethod.Post))
{
    <div style="margin-left: 30px;">
        @if (Model != null && Model.Count > 0)
        {

            for (int i = 0; i < Model.Count; i++)
            {
                <div>
                    @Html.HiddenFor(m=>Model[i].QuestionId)
                    <span>@Model[i].QuestionName</span>
                    @for (int j = 0; j < Model[i].Options.Count(); j++)
                    {
                        <div>  @Html.RadioButtonFor(m=>Model[i].SelectedOption,Model[i].Options[j].OptionId)
                            <span>@Model[i].Options[j].OptionName</span>
                        </div>
                    }
                </div>
            }
        }


        <input type="submit" value="Submit Answer"  />

        <input type="button" value="End Test" />

    </div>
}

@{
    int cnt = 0;
    int sel = 0;
    int t_id = 0;
  }


@{
    sel = ViewBag.ct;
    cnt = ViewBag.total;
    t_id = ViewBag.TestId;
  }
<span style="margin-left:350px">
   @Html.ActionLink("First", "TestStarted", new { cnt = 0,TestId=t_id }, new { style = "color:#000;display:inline-block;width:60px;padding:10px;border:1px solid #CCC;text-align:center;text-decoration:none;margin-left:5px;background-color:#F8F7FC;border-radius:10px;" })
@for (int i = 0; i < (cnt / 1); i++)
{
    if (sel == i)
    {
        @Html.ActionLink((i + 1).ToString(), "TestStarted", new { cnt = i,TestId=t_id }, new { style = "color:#000;display:inline-block;width:40px;padding:10px;border:1px solid #CCC;text-align:center;text-decoration:none;margin-left:5px;background-color:#CCC;border-radius:10px;" })
    }
    else
    {
        @Html.ActionLink((i + 1).ToString(), "TestStarted", new { cnt = i,TestId=t_id }, new { style = "color:#000;display:inline-block;width:40px;padding:10px;border:1px solid #CCC;text-align:center;text-decoration:none;margin-left:5px;background-color:White;border-radius:10px;" })
    }
}
 @Html.ActionLink("Last", "TestStarted", new { cnt = (cnt / 1) - 1,TestId=t_id }, new { style = "color:#000;display:inline-block;width:60px;padding:10px;border:1px solid #CCC;text-align:center;text-decoration:none;margin-left:5px;background-color:#F9F9F9;border-radius:10px;" })

</span>
我得到了我的所有问题及其在列表中选择的答案:

模型=新测试计划。通过测试ID获取问题

但问题在于查看页面的这一行:

@model List<EAssessmentNew.Models.Question>

@{
    ViewBag.Title = "TestStarted";
    Layout = "~/Views/StudentMaster.cshtml";
}

@using (Html.BeginForm("TestStarted","Student",FormMethod.Post))
{
    <div style="margin-left: 30px;">
        @if (Model != null && Model.Count > 0)
        {

            for (int i = 0; i < Model.Count; i++)
            {
                <div>
                    @Html.HiddenFor(m=>Model[i].QuestionId)
                    <span>@Model[i].QuestionName</span>
                    @for (int j = 0; j < Model[i].Options.Count(); j++)
                    {
                        <div>  @Html.RadioButtonFor(m=>Model[i].SelectedOption,Model[i].Options[j].OptionId)
                            <span>@Model[i].Options[j].OptionName</span>
                        </div>
                    }
                </div>
            }
        }


        <input type="submit" value="Submit Answer"  />

        <input type="button" value="End Test" />

    </div>
}

@{
    int cnt = 0;
    int sel = 0;
    int t_id = 0;
  }


@{
    sel = ViewBag.ct;
    cnt = ViewBag.total;
    t_id = ViewBag.TestId;
  }
<span style="margin-left:350px">
   @Html.ActionLink("First", "TestStarted", new { cnt = 0,TestId=t_id }, new { style = "color:#000;display:inline-block;width:60px;padding:10px;border:1px solid #CCC;text-align:center;text-decoration:none;margin-left:5px;background-color:#F8F7FC;border-radius:10px;" })
@for (int i = 0; i < (cnt / 1); i++)
{
    if (sel == i)
    {
        @Html.ActionLink((i + 1).ToString(), "TestStarted", new { cnt = i,TestId=t_id }, new { style = "color:#000;display:inline-block;width:40px;padding:10px;border:1px solid #CCC;text-align:center;text-decoration:none;margin-left:5px;background-color:#CCC;border-radius:10px;" })
    }
    else
    {
        @Html.ActionLink((i + 1).ToString(), "TestStarted", new { cnt = i,TestId=t_id }, new { style = "color:#000;display:inline-block;width:40px;padding:10px;border:1px solid #CCC;text-align:center;text-decoration:none;margin-left:5px;background-color:White;border-radius:10px;" })
    }
}
 @Html.ActionLink("Last", "TestStarted", new { cnt = (cnt / 1) - 1,TestId=t_id }, new { style = "color:#000;display:inline-block;width:60px;padding:10px;border:1px solid #CCC;text-align:center;text-decoration:none;margin-left:5px;background-color:#F9F9F9;border-radius:10px;" })

</span>
@模型列表

在控制器端执行此行时:

返回Viewmodel.Skip0.1

它抛出了错误

传递到字典中的模型项的类型为“System.Linq.Enumerable+d_uu3A1[EAssessmentNew.Models.Question]”,但此字典需要类型为“System.Collections.Generic.List1[EAssessmentNew.Models.Question]”的模型项

skip方法返回类型为ienumerable,并且我已在查看页面上获取了Take列表

有谁能为我提供解决方案吗


这个错误消息清楚地解释了您正在向视图传递一个可枚举项,但是视图需要一个列表

尝试在return语句中添加ToList。例如:

return View(model.Skip(0).Take(1).ToList());