繁体   English   中英

在EditorTemplates中保持计数

[英]Keeping count within EditorTemplates

使用EditorTemplates(With Lists)计算当前列表索引值是否有办法? 例如,在此编辑器模板中

@model myModel

<div class="email-box">
    @Html.TextBoxFor(x=>x.Email)
</div>

我可以找出列表中的索引吗?

EDIt ====

视图

 @model IEnumerable<myModel>

 @Html.EditorForModel()

编辑模板

@model myModel
<div class="email-box" rel="@ViewBag.Index">
    @Html.TextBoxFor(x => x.Email)
</div>

从Html扩展方法调用时,自定义编辑器模板可以接受值,这些值将专门添加到ViewBag 您可以使用它将元素的索引发送到该视图

在父视图中

@{
    ViewBag.Title = "MyView";
    var index = 0;
}

<h2>My View</h2>

@foreach (var item in Model)
{
    <div>
        @Html.EditorFor(m => item, new { Index = index++ })
    </div>
}

在编辑器模板中

@Html.TextBoxFor(m => string.Format("({0} - {1}", ViewBag.Index, m.Description)

我实际上在查看可用的字段选项时解决了这个问题。

ViewData.TemplateInfo.HtmlFieldPrefix

返回[0],[1]等。可以减少它,我们有ID。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM