12

如何将 Tab Order 属性用于以下代码:

<td>
    @Html.EditorFor(model => model.Cost)                
</td>

我试过这个:

<td tabindex=1>
    @Html.EditorFor(model => model.Cost)                
</td>

有什么建议么?

4

5 回答 5

14

您还可以在帮助程序本身中指定相同的 html 属性,如下所示。

@Html.TextBoxFor(model => model.Cost, new { tabindex = 1 })
于 2011-09-29T03:18:56.530 回答
10

与@Stuy1974 的正确答案相反,如果您不想离开EditorFor解决方案,则必须连接自己的Editor Template

@ModelType SomeApp.ViewModels.SomeNiftyViewModel 

// be sure to include the TabIndex info in the ViewModel
@Html.TextBoxFor(model => model.Cost, new { tabindex = model.TabIndex })

您还可以直接使用已经传递给编辑器模板的 ViewData 参数,而不是向模型添加选项卡索引:

// In the main view
@Html.EditorFor(model => model.Cost, new { TabIndex = 3 })

// In the editor template
@{ int tabIndex = (ViewData["TabIndex"] as int?) ?? 0; }
@Html.TextBoxFor(model => model, new { tabindex = tabIndex })
于 2012-02-14T01:46:07.330 回答
1

只需这样做

@Html.EditorFor(model => model.Cost,  new { htmlAttributes = new { tabindex = 34 } })
于 2014-07-29T10:55:48.420 回答
1

允许您保留 EditorFor 的另一个选项是在页面加载后在 javascript 中设置选项卡索引,例如:

var myEditorFor = document.getElementById("MyEditorForId");

if (myEditorFor != null) {
    myEditorFor.setAttribute("tabindex","18")
}
于 2015-10-22T17:09:31.600 回答
0

不幸的是 @Html.EditorFor 方法不提供添加 HTML 属性的能力。您可以通过更具体的方法调用添加这些。在上述情况下,我会使用 -

@Html.TextBoxFor(model => model.Cost, new { tabindex = 1 })

于 2012-02-14T01:38:15.147 回答