如何将 Tab Order 属性用于以下代码:
<td>
@Html.EditorFor(model => model.Cost)
</td>
我试过这个:
<td tabindex=1>
@Html.EditorFor(model => model.Cost)
</td>
有什么建议么?
如何将 Tab Order 属性用于以下代码:
<td>
@Html.EditorFor(model => model.Cost)
</td>
我试过这个:
<td tabindex=1>
@Html.EditorFor(model => model.Cost)
</td>
有什么建议么?
您还可以在帮助程序本身中指定相同的 html 属性,如下所示。
@Html.TextBoxFor(model => model.Cost, new { tabindex = 1 })
与@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 })
只需这样做
@Html.EditorFor(model => model.Cost, new { htmlAttributes = new { tabindex = 34 } })
允许您保留 EditorFor 的另一个选项是在页面加载后在 javascript 中设置选项卡索引,例如:
var myEditorFor = document.getElementById("MyEditorForId");
if (myEditorFor != null) {
myEditorFor.setAttribute("tabindex","18")
}
不幸的是 @Html.EditorFor 方法不提供添加 HTML 属性的能力。您可以通过更具体的方法调用添加这些。在上述情况下,我会使用 -
@Html.TextBoxFor(model => model.Cost, new { tabindex = 1 })