我在这里有一个.NET Core 2.0应用程序,在我的一种观点中,我使用了foreach
循环来显示此数据库表中的所有项目。一切都很好。现在,我在Bootstrap模态中包含的另一个foreach
循环中显示了ID列表,但是现在我看到了一条错误消息
名称“ modelItem”在当前上下文中不存在
我确定原因。看起来一切正常。有人可以指出为什么我遇到此错误,或者提供一种替代方法从数据库中提取所需信息以这种方式显示吗?
查看
@foreach (var Submission in Model.Submissions)
{
<div class="col-sm-3">
// Code that populates the project cards on the page
</div>
<!-- Modal -->
<div class="modal fade" id="StatusCodeUpdate-@Submission.ID" tabindex="-1" role="dialog" aria-labelledby="StatusCodeUpdateLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="StatusCodeUpdateLabel">Update Status Code for @Submission.Title </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h6>Status Codes</h6>
<form method="post">
@foreach (var Code in Model.StatusCodes)
{
@Html.DisplayFor(modelItem = Code.ID) // ERROR!!!
}
</form>
</div>
...
}
隐藏代码
public class TeamleadModel : PageModel
{
public IList<Submission> Submissions { get; set; }
public IList<Status> StatusCodes { get; set; }
private readonly SubmissionContext _submissionContext;
private readonly StatusContext _statusContext;
public TeamleadModel(SubmissionContext submission, StatusContext statusContext)
{
_submissionContext = submission;
_statusContext = statusContext;
}
public async Task OnGetAsync()
{
Submissions = await _submissionContext.Submissions.ToListAsync();
StatusCodes = await _statusContext.StatusCodes.ToListAsync();
}
答案 0 :(得分:1)
尝试@Html.DisplayFor(modelItem => Code.ID)
。您有一个分配而不是lambda表达式。