Tab Order in ASP.NET MVC 3 Helpers

寵の児 提交于 2019-11-30 22:55:43

问题


How can I use Tab Order property for following code:

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

I tried this:

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

any suggestions?


回答1:


You can also specify the same html attribute in the helper itself as follows.

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



回答2:


As a contrast to @Stuy1974's right answer, if you don't want to leave the EditorFor solution, you're going to have to wire up your own 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 })

You can also use the ViewData parameter already passed to the editor template directly rather than adding the tab index to the model:

// 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 })



回答3:


Simply do this

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



回答4:


Another option, allowing you to retain the EditorFor, is to set the tab index in javascript after the page has loaded with something like:

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

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



回答5:


Unfortunately @Html.EditorFor method doesn't provide the ability to add HTML attributes. You can add these via a more specific method call. In the above case I'd use -

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



来源:https://stackoverflow.com/questions/6679013/tab-order-in-asp-net-mvc-3-helpers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!