我有一個 IEnumerable 模型,想要在視圖中一一顯示。
public IActionResult Index()
{
var model = _context.Question.ToList();
return View(model);
}
我有一個類似于下面的觀點。我希望通過按下下一個和上一個按鈕來顯示下一個和上一個專案

這是我的觀點
<div>
<p>
@foreach (var item in Model.Take(1))
{
@Html.Raw(item.QuestionContent)
foreach (var item2 in item.Answers)
{
@Html.Raw(item2.AnswerContent)
<input type="checkbox" />
}
}
</p>
<div>
<a id="btnnext" class="btn">
Next
</a>
<a id="btnprev" class="btn">
Prev
</a>
</div>
uj5u.com熱心網友回復:
這是您可以遵循的完整作業演示:
模型:
public class Question
{
public int Id { get; set; }
public string QuestionContent { get; set; }
public List<Answer> Answers { get; set; }
}
public class Answer
{
public int Id { get; set; }
public string AnswerContent { get; set; }
}
看法:
@model IEnumerable<Question>
@{
int current = (int)ViewBag.CurrentIndex;
}
<div>
<p>
@Html.Raw(Model.ElementAt(current).QuestionContent)
@foreach (var item in Model.ElementAt(current).Answers)
{
@Html.Raw(item.AnswerContent)
<input type="checkbox" />
}
</p>
@{
var prevDisabled = !(bool)ViewBag.HasPreviousPage ? "disabled" : "";
var nextDisabled = !(bool)ViewBag.HasNextPage ? "disabled" : "";
}
<a asp-action="Index"
asp-route-index="@ViewBag.PreviousIndex"
class="btn btn-default @prevDisabled">
Previous
</a>
<a asp-action="Index"
asp-route-index="@ViewBag.NextIndex"
class="btn btn-default @nextDisabled">
Next
</a>
</div>
控制器:
public IActionResult Index(int index)
{
var model = _context.Question.ToList();
ViewBag.CurrentIndex = index;
ViewBag.PreviousIndex = index - 1;
ViewBag.NextIndex = index 1;
ViewBag.HasPreviousPage = ViewBag.PreviousIndex == -1 ? false : true;
ViewBag.HasNextPage = ViewBag.NextIndex == model.Count() ? false : true;
return View(model);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/327186.html
標籤:javascript 查询 asp.net asp.net核心
上一篇:在Web表單專案中,TreeView未顯示從函式呼叫到OnSelectedNodeChange的選中節點
下一篇:[Vue警告]:無效的道具:道具“eventKey”的型別檢查失敗。預期的字串、數字、得到陣列并避免使用非原始值作為鍵
