EditorFor 忽略 tabindex。如何设置 tabindex?

发布于 2024-10-18 00:38:57 字数 412 浏览 8 评论 0原文

tabindex 的使用似乎只适用于像 Textboxfor 这样的 htmlhelpers,而不适用于 EditorFor

例如;

<%: Html.TextBoxFor(model => Model.MyItem, new { @tabindex = "3" })%>

产生一个 tabindex 值。

但是,如果您使用;

<%: Html.EditorFor(model => Model.MyItem, new { @tabindex = "3" })%>

那么结果就是控件按预期创建了,但是缺少tabindex。

那么……如何为给定的EditorFor控件设置tabindex呢?

The use of tabindex seems to only work for htmlhelpers like Textboxfor and not EditorFor

For example;

<%: Html.TextBoxFor(model => Model.MyItem, new { @tabindex = "3" })%>

Produces a tabindex value.

However, if you use;

<%: Html.EditorFor(model => Model.MyItem, new { @tabindex = "3" })%>

Then the result is that the control is created as expected, but the tabindex is missing.

So...... How is it possible to set the tabindex for a given EditorFor control?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦欢 2024-10-25 00:38:57

我遇到的主要问题是我需要创建一个 EditorFor 类型机制,以便像货币一样格式化小数(我们的系统有多种货币,所以“C”不合适),让选项卡索引工作并允许系统维护标准验证。

我已经成功地使用以下方法实现了这一点。通过创建我自己的自定义编辑器控件。

在项目的 Views/Shared/EditorTemplates 目录中创建一个文件(我的文件名为decimal.ascx)。

 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<decimal?>" %>
 <% int intTabindex = 0;
   decimal myVal = 0;

   string strModelValue = "";
   if (Model != null)
   {
       myVal = (decimal)Model;
       strModelValue = myVal.ToString("#.00");
   }
   else
       strModelValue = "";



   if (ViewData["tabindex"] != null)
   {
       intTabindex = (int)ViewData["tabindex"];
   }
 %>
 <%: Html.TextBox("", strModelValue, new { @tabindex = intTabindex })%>

本质上,这段代码只是覆盖了通常在“十进制”EditorFor 方法中显示的内容;

<%: Html.TextBox("", Model.ToString("#.00"), new { @tabindex = intTabindex }) %>

模板。

我的调用代码现在为:

<%: Html.EditorFor(model => Model.MyItem, new { tabindex = 5 })%>

结果是页面上的以下代码。

<input id="Model_MyItem" name="Model.MyItem" tabindex="5" type="text" value="12.33" />

这正是我所需要的。

虽然这仅适用于我的特定情况,但我鼓励任何想要解决此问题的人首先尝试为该任务使用自定义控件,因为它可能会为您节省大量时间。

当然,如果可以在代码中创建所需的特定类型的控件并围绕该控件调整结果。

例如;我们可以简单地在调用中添加另一个项目来确定文本格式。

new {tabindex = 12, numberformat=2}

然后只需为所有格式创建一个处理程序。

The main problem I was having is that I needed to create a EditorFor type mechanism in order to format the decimal like a currency (our system has multiple currencies so "C" would not have been appropriate), get a tab index working AND allow the system to maintain the standard validation.

I've managed to achieve that using the following. By creating my own custom editor control.

Create a file (mine is called decimal.ascx) within the Views/Shared/EditorTemplates directory of your project.

 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<decimal?>" %>
 <% int intTabindex = 0;
   decimal myVal = 0;

   string strModelValue = "";
   if (Model != null)
   {
       myVal = (decimal)Model;
       strModelValue = myVal.ToString("#.00");
   }
   else
       strModelValue = "";



   if (ViewData["tabindex"] != null)
   {
       intTabindex = (int)ViewData["tabindex"];
   }
 %>
 <%: Html.TextBox("", strModelValue, new { @tabindex = intTabindex })%>

Essentially, this code just overrides what would normally be presented in a "decimal" EditorFor method with the;

<%: Html.TextBox("", Model.ToString("#.00"), new { @tabindex = intTabindex }) %>

template.

My calling code now reads;

<%: Html.EditorFor(model => Model.MyItem, new { tabindex = 5 })%>

The result is the following code on the page.

<input id="Model_MyItem" name="Model.MyItem" tabindex="5" type="text" value="12.33" />

Which is exactly what I required.

Whilst this is only true for my particular circumstances, I would encourage anybody looking to solve this issue to attempt a custom control first for the task as it might save you a considerable amount of time.

If would of course be possible in the code to create a specific type of control required and adjust the results around that.

For example; we could simple add another item in the call to determine the text format.

new {tabindex = 12, numberformat=2}

Then simply create a handler for all the formats.

御弟哥哥 2024-10-25 00:38:57

由于 EditorFor 只是数据类型的模板,因此它只需要一个数据类型,因为它是模型。我猜有几种方法可以解决这个问题。您可以将 tabindex 添加到匿名对象,该对象将合并到 EditorTemplate 的 ViewData 中,如下所示。

视图中的代码:

Html.EditorFor(m => m.Username, "test", new { tabindex = 3, style = "width: 400px;" })

EditorForModel 模板检查 ViewData:

<%: Html.TextBoxFor(m => m.Username, ViewData)%>

这应该呈现 tabindex 为 3 且 style="width: 400px;" 的文本输入

快乐编码。

编辑:
这正是我在测试页中的标记:

<%: Html.EditorFor(m => m.DollarsAmount, "NullableDecimal", new { tabindex = 99 }) %>

我告诉 Edi​​torFor 模板选择我创建的“NullableDecimal”EditorTemplate。 (您可以在模型内部的属性上放置一个 UiHint 属性,也告诉它要使用什么编辑器模板)

“NullableDecimal”EditorTemplate 位于 ~/Views/Shared/EditorTemplates 中:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<decimal?>" %>

<%: Html.TextBox(string.Empty, (Model.HasValue ? Model.Value.ToString("#.00") : null), ViewData) %>

我的实现更可扩展的是我传递的额外 ViewData通过我的匿名对象合并到 ViewData 字典中以供 EditorTemplate 使用。因此,如果您不将任何 ViewData 传递到 EditorTemplate,那么它不会像您当前的实现那样将您的文本输入 tabindex 呈现为 0。另外,您的实现将仅考虑选项卡索引,而不考虑任何其他输入属性。即最大长度或样式

Since the EditorFor is just a template for a DataType it's only expecting a datatype as it's Model. There are a couple of ways of going about this I am guessing. You could possibly add the tabindex to an anonymous object that will be merged into the ViewData for the EditorTemplate like so.

Code in your View:

Html.EditorFor(m => m.Username, "test", new { tabindex = 3, style = "width: 400px;" })

EditorForModel Template Check for ViewData:

<%: Html.TextBoxFor(m => m.Username, ViewData)%>

This should render an text input with a tabindex of 3 and style="width: 400px;"

Happy coding.

Edited:
Here is exactly the markup I have inside of my test page:

<%: Html.EditorFor(m => m.DollarsAmount, "NullableDecimal", new { tabindex = 99 }) %>

I'm telling the EditorFor template to select the "NullableDecimal" EditorTemplate that I have created. (You could place a UiHint attribute on the property inside of the model also to tell it what editortemplate to use)

"NullableDecimal" EditorTemplate located in ~/Views/Shared/EditorTemplates:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<decimal?>" %>

<%: Html.TextBox(string.Empty, (Model.HasValue ? Model.Value.ToString("#.00") : null), ViewData) %>

What's more extensible about my implementation is the extra ViewData that I pass in via my anonymous object is merged into the ViewData dictionary to be used by the EditorTemplate. So if you don't pass any ViewData in to the EditorTemplate then it wont render your text inputs tabindex to 0 as your implementation currently will do. Plus your implementation will only account for tabindexes and not for any other input attributes. i.e. maxlength or style

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文