显示从列表到剃须刀视图的数据



我想向剃须刀视图显示我的列表(字符串(的所有值,但不使用" foreach"方法。

我已经知道了,但是剩下的唯一的th是List<Answer>

   @Model PoolManager.Models.Question
 @{
ViewData["Title"] = "Details";
}
<h2>Details</h2>
<table class="table">
<thead>
    <tr class="bg-primary">
        <th>Question</th>
        <th>Answers</th>
        <th>Starting Date</th>
        <th>Ending Date</th>
        <th>Active</th>
        <th>Details</th>
        <th>Edit</th>
        <th>Delete</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td class="bg-info">@Model.Text</td>
        @for (int i = 0; i < Model.Answers.Count; i++)
        {
            String Resposta = Model.Answers[i].ToString();
            <td class="bg-info" @Resposta></td>
        }
        <td class="bg-info">@Model.StartDate</td>
        <td class="bg-info">@Model.EndDate</td>
        <td>
            <a asp-action="Edit" asp-route-id="@Model">Edit</a> |
            <a asp-action="Details" asp-route-id="@Model.Id">Details</a> |
            <a asp-action="Delete" asp-route-id="@Model.Id">Delete</a>
        </td>
    </tr>
</tbody>

我到目前为止有这个,但它仅计算答案的数量,但是我需要输出答案而不是它的ID。

  @for (int i = Model.Answers.Count; i >= 0; i--)
  {          
      String Resposta = Model.Answers.ToString();
      <td class="bg-info" @Resposta></td>
  }

输出IM获得:

system.collections.generic.list`1 [poolmanager.models.answer]

首先要修复表格部分,现在看起来不好。

第二,如果您只想显示1个项目,则不能使用表。

第三;在您的控制器的

ActionResult MyPage(){}

方法,您可以通过临时用法发送答案列表。

public ActionResult MyPage(){
     List<PoolManager.Models.Question> questionList = dbModel.Questions.ToList();
     TempData['answerList'] = dbModel.Answers/*.Where(//IF YOU WANT A WHERE CLAUSE YOU CAN WRITE HERE AS WELL)*/.ToList();
     //We just sent List<Answers> inside that TempData["answerList"], we will cast it in RazorPage later.
     return View(questionList);
}

这样做之后,您应该使用

@model List<PoolManager.Models.Question>

在剃须刀页面的顶部。因为我们正在发送PoolManager.Models.Question对象的列表。在这里,我假设您正在使用Bootstrap模式。如果没有,您可以将其用作每个答案单元格的内部和这些div中的另一个表。让我们看一下我的方式:

<table class="table">
    <thead>
        <tr class="bg-primary">
            <th>Question</th>
            <th>Answers</th>
            <th>Starting Date</th>
            <th>Ending Date</th>
            <th>Active</th>
            <th>BUTTON LINKS</th>
        </tr>
    </thead>
    <tbody>
        @foreach(var question in Model)
        {
        <tr>
            <td class="bg-info">@question.Text</td>
            <td class="bg-info">
                <a href="#" class="btn btn-primary" data-target="#myModalId_@question.questionId" data-toggle="modal">Answers</a>
            </td>
            <td class="bg-info">@question.StartDate</td>
            <td class="bg-info">@question.EndDate</td>
            <td></td>
            <td>
            <a asp-action="Edit" asp-route-id="@question">Edit</a> |
            <a asp-action="Details" asp-route-id="@question.Id">Details</a> | 
            <a asp-action="Delete" asp-route-id="@question.Id">Delete</a>
            </td>
        </tr>
        }
    </tbody>
</table>

我们刚刚完成表部分,让我们填写模式。您需要将tempdata [" answerlist"]施加到列表我们之前在控制器中描述的类。

@foreach(var question in Model)
{
    <div class="modal fade" id="myModalId_@question.questionId" tabindex="-1" role="dialog" aria-labelledby="myModalId_@question.questionId">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalId_@question.questionId">Looking at @question.Text 's answers</h4>
                </div>
                <div class="modal-body">
                    @foreach(var answer in ((List<Answers>)TempData["answerList"]).ToList() )
                    {
                        <div class="row">
                            @answer.Text
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>
}

我希望这对您有很大帮助。

内部循环您可以使用索引在列表中的每个答案中获取值。例如:

 @for (int i = Model.Answers.Count; i >= 0; i--)
    {
        string Resposta = Model.Answers[i].ToString();
        <td class="bg-info">@Resposta</td>
    }

上面的代码将帮助您获取使用循环本身在每个索引上存储的值。

最新更新