【发布时间】:2015-05-19 14:52:31
【问题描述】:
目前我有一个视图,它采用一个 IEnumerable 模型,用于在视图上显示数据。然而,在同一个视图中,我还有一个模态弹出窗口,我想在其中添加到模型中,而不是将它们分成不同的视图。我试图遵循这个问题底部的建议How to access model property in Razor view of IEnumerable Type?,但遇到了一个例外
表达式编译器无法评估索引器表达式“(model.Count - 1)”,因为它引用了不可用的模型参数“model”。
在我的视图顶部
@model IList<Test.Models.DashModel>
在我的模态体内我有
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>DashboardModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model[model.Count - 1].DashName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model[model.Count - 1].DashName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model[model.Count - 1].DashName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model[model.Count - 1].CreatedDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model[model.Count - 1].CreatedDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model[model.Count - 1].CreatedDate, "", new { @class = "text-danger" })
</div>
</div>
</div>
}
【问题讨论】:
-
试试 Model.Count(带大写 M)
-
既然你的视图有一个只能编辑一个对象的表单,那你到底为什么要向视图传递一个集合而不是一个对象
标签: c# asp.net-mvc-5