我想做有问题的分页dta是我想每页显示1个问题。
这是我的模特:
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>
我在这个列表中的选定答案中得到了所有问题:
model = new Test_Planning()。Fetch_Question_By_Test(TestId);
但问题出现在视图页面的这一行:
@model List
在控制器端执行此行时:
返回视图(model.Skip(0).Take(1));
它会抛出错误。
传入字典的模型项类型为'System.Linq.Enumerable + d _3a 1[EAssessmentNew.Models.Question]', but this dictionary requires a model item of type 'System.Collections.Generic.List
1 [EAssessmentNew.Models.Question]'。
跳过方法返回类型是不可数的,我已经在我的视图页面上采取了列表。
任何人都可以为我提供解决方案吗?
我对此非常感激
答案 0 :(得分:0)
错误消息清楚地说明您将Enumerable
传递给视图,但视图需要List
。
尝试在ToList()
语句中添加return
。例如:
return View(model.Skip(0).Take(1).ToList());