C# 在EditorTemplates中保持计数
使用EditorTemplates(带列表)计算当前列表索引值时,有没有方法?例如,在这个编辑器模板中C# 在EditorTemplates中保持计数,c#,asp.net-mvc,C#,Asp.net Mvc,使用EditorTemplates(带列表)计算当前列表索引值时,有没有方法?例如,在这个编辑器模板中 @model myModel <div class="email-box"> @Html.TextBoxFor(x=>x.Email) </div> @model myModel <div class="email-box" rel="@ViewBag.Index"> @Html.TextBoxFor(x => x.Email
@model myModel
<div class="email-box">
@Html.TextBoxFor(x=>x.Email)
</div>
@model myModel
<div class="email-box" rel="@ViewBag.Index">
@Html.TextBoxFor(x => x.Email)
</div>
@model-myModel
@Html.TextBoxFor(x=>x.Email)
我能计算出我在列表中的索引吗
编辑====
看法
@model IEnumerable
@Html.EditorForModel()
编辑器模板
@model myModel
<div class="email-box">
@Html.TextBoxFor(x=>x.Email)
</div>
@model myModel
<div class="email-box" rel="@ViewBag.Index">
@Html.TextBoxFor(x => x.Email)
</div>
@model-myModel
@Html.TextBoxFor(x=>x.Email)
当从Html扩展方法调用时,自定义编辑器模板可以接受值,这些值被添加到专门用于该视图的视图包中。您可以使用它将元素的索引发送到该视图中
父视图中的
@{
ViewBag.Title = "MyView";
var index = 0;
}
<h2>My View</h2>
@foreach (var item in Model)
{
<div>
@Html.EditorFor(m => item, new { Index = index++ })
</div>
}
实际上,我在查看可用字段选项时解决了这个问题
ViewData.TemplateInfo.HtmlFieldPrefix
返回[0]、[1]等。。我已经更新了这个问题,但在这种情况下不行,因为我从未在模型中迭代对象。