如何获得序列/数组索引在编辑模板? [英] How to get sequence/array index in Editor Template?
本文介绍了如何获得序列/数组索引在编辑模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
案例:
我一直在使用编辑模板X类显示类X的项目清单。
Case: I have a list of items of Class X displayed using Editor Template for Class X.
问题:
我怎样才能在编辑模板内部正在处理项目的索引?
Problem: How can I get index of an item being processed on the inside of the Editor Template?
推荐答案
使用for循环,而不是为每索引器传递到 EditorFor
扩展;剃刀应该处理的其余部分。
Use a for loop instead of for each and pass the indexer into the EditorFor
extension; razor should handle the rest.
@for(var i = 0; i < Model.count(); i++)
{
@Html.EditorFor(m => Model.ToArray()[i], new { index = i })
}
更新:
通使用视图数据如上显示该项目的索引。
pass in the the index of the item using view data as show above.
在编辑器模板访问通过ViewBag项目
In your editor template access the item via the ViewBag
<span> Item Index: @ViewBag.index </span>
这篇关于如何获得序列/数组索引在编辑模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文