2011-07-13 57 views
12

如何,我可以使用Tab键顺序属性下面的代码:Tab顺序

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

我尝试这样做:

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

有什么建议?

+3

上次见到@AbdullahSaqib是在2011年问这个问题后的5天 –

回答

13

您也可以在助手本身中指定相同的html属性,如下所示。

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

这在'EditorFor'中不起作用,EditorFor类型对象中的第二个参数用于构建View Data而不是添加HTML属性根据文档:http://msdn.microsoft.com/en-us/library/ff406462.aspx –

+0

您不能在EditorFor中指定其他参数。 –

+1

令人印象深刻的是upvotes唯一的答案是不正确的。 –

0

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

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

+3

他不必离开EditorFor。他可以轻松创建一个编辑器模板。 –

11

作为对比@ 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 }) 
+1

感谢您的想法,完美工作。 –

1

只要做到这一点

@Html.EditorFor(model => model.Cost, new { htmlAttributes = new { tabindex = 34 } }) 
1

另一种选择,允许你保留EditorFor,就是在页面加载类似这样的东西后在javascript中设置标签索引:

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

if (myEditorFor != null) { 
    myEditorFor.setAttribute("tabindex","18") 
}