简体   繁体   English

Asp.net core 如何一一显示IEnumerable模型

[英]Asp.net core how to display IEnumerable model one by one

I have an IEnumerable model that want to show in one by one item in view.我有一个 IEnumerable 模型,想要在视图中一一显示。

   public IActionResult Index()
   {
       var model = _context.Question.ToList();
       return View(model);
   }

I have a view similar to the one below.我有一个类似于下面的观点。 I want the next and previous items to be displayed by pressing the next and previous button我希望通过按下下一个和上一个按钮来显示下一个和上一个项目

在此处输入图片说明

And this is my view这是我的观点

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

Here is a whole working demo you could follow:这是您可以遵循的完整工作演示:

Model:模型:

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

View:看法:

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

Controller:控制器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 asp.net mvc 视图中从 ienumerable 到 select 一行 - How to select one row from ienumerable in asp.net mvc view 在 Asp.Net Core 3 web 应用程序中只显示一次模态 - Only display modal one time in Asp.Net Core 3 web app 如何在用asp.net mvc核心制作的应用程序的一两页中将angular 4用作迷你spa? - How to use angular 4 as mini spa in just one or two pages of an application made with asp.net mvc core? 如何使用react应用程序将一个asp.net核心控制器操作视图访问到iframe中? - How to access a one of the asp.net core controller action view into an iframe using react application? 如何在asp.net Core中将模型对象传递给javascript函数 - How to pass model object to javascript function in asp.net Core 一次从一种形式添加许多人并保存在asp.net核心中的一张表中 - Add many people from one form at a time And save in one table in asp.net core 如何更新ASP.NET MVC中的一个HTML元素? - How to update one HTML element in ASP.NET MVC? 如何在asp.net中创建一个一次性注册页面? - How to create a one time registration page in asp.net? 如何在ASP.NET中选择多个图像按钮 - How to select more than one image button in ASP.NET 如何在asp.net中打开新窗口并禁用旧窗口? - How to open new window in asp.net and disable the old one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM