gpt4 book ai didi

html - 在 MVC razor 中显示来自模型的列表

转载 作者:搜寻专家 更新时间:2023-10-31 22:13:32 24 4
gpt4 key购买 nike

我可以从 Razor 模型中枚举一个列表吗?

我的模型:

public class PreapprovalViewModel
{
public int ProjectId { get; set; }
public int Decision { get; set; }
public List<BranchHead> BranchHeads { get; set; }
public List<SelectListItem> Decisions { get; set; }
}

在 Preapproval 的索引上,我想要一个显示 BranchHead 列表中某些字段的表格。

Razor :

@model Mars.Intranet.ViewModels.PreapprovalViewModel
@{
ViewBag.Title = "Index";
}
@Html.Partial("_ProjectPartial")
<h2>Preapproval</h2>
@using (Html.BeginForm()) {
<div>
Approve research request:
@Html.DropDownListFor(o => o.Decision, Model.Decisions)
</div>
<div id="assign-branch-head" style="display: none;">
Assign branch heads
<table>
<tr>
<th>@Html.DisplayFor(o => o.BranchHeads.BranchName</th> <!-- Can I access the list in the model like this?-->
<th>@Html.DisplayFor(o => p.BranchHeads.FirstName</th> <!-- Can I access the list in the model like this?-->
<th>@Html.DisplayFor(o => p.BranchHeads.Comment</th> <!-- Can I access the list in the model like this?-->
</tr>
<tr>
<td><!-- I want the info from the list in model here--></td>
<td><!-- I want the info from the list in model here--></td>
<td><!-- I want the info from the list in model here--></td>
</tr>
</table>
</div>
<div class="approval-comment">
Comment:
@Html.TextAreaFor(o => o.Comment)
</div>
<button type="submit" class="btn_submit"></button>
}

最佳答案

是的,这完全有可能。但由于这是一个 List<T>您需要对其进行迭代才能显示数据。

只需将您的数据包装在一个 foreach 循环中

@foreach (var item in Model.BranchHeads)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.BranchName)<br/>
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)<br/>
</td>
<td>
@Html.DisplayFor(modelItem => item.Comment)<br/>
</td>

</tr>
}

此外,您可能想使用 Html.DisplayNameFor()显示表格标题。

关于html - 在 MVC razor 中显示来自模型的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21852798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com