目前我有一个视图,它采用IEnumerable模型,我用它来在视图上显示数据。然而,在相同的观点,我也有一个模式弹出,我想添加到模型,而不是将它们分离到不同的意见。我试图按照在这个问题How to access model property in Razor view of IEnumerable Type?底部的建议,但有一个例外无法访问模型(vs模型)的属性?
表达编译器无法评估索引表达式“(model.Count - 1)”,因为它引用了模型参数“模式'这是不可用的。
在视图的顶部,我有
@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) – Carl
既然你查看有一个表单只能编辑一个对象,那么究竟为什么你传递一个集合的观点,而不是一个对象 –