禁用 MVC 中 html 帮助文本框的自动完成功能
好的,
我会在正常的 ASP.NET 中使用主题来关闭整个站点上所有文本框的自动完成功能。但是我无法在 MVC 上执行此操作,因为主题 .skin 文件中的任何内容似乎都不起作用。
我的 .skin 文件中有这个:
<asp:TextBox runat="server" autocomplete="off" />
但是这根本不会渲染,当然,因为这不是 MVC 的工作方式。无论如何,有什么办法可以让这种事情发挥作用。 我尝试执行此操作的网站太大,无法保证更改每个文本框或创建新的 HTML 帮助程序来解决问题?
有人有什么想法吗?
Ok,
I would in normal asp.net use a theme to turn off autocomplete on all text boxes on an entire site. However i cannot do this on MVC because nothing in the theme .skin files seems to work.
I have this in my .skin file:
<asp:TextBox runat="server" autocomplete="off" />
however this does not render at all, of course because this is not how MVC works. Anyway is there any way i can get this sort of thing to work.
The site i am trying to do it on is too big to warrant changing every textbox or creating a new HTML helper to solve the issue?
Anyone got any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
MVC 没有像普通的旧 ASP.NET 那样的服务器控件。因此,您的控件上不会进行任何服务器处理。它们将按照您键入的方式呈现给客户端。主题不是您将在 MVC 中使用的内容,因为它们适用于 ASP.NET 服务器控件,并且您不会在此处使用它们。也就是说,HTML 助手确实会在视图呈现时由服务器进行处理。您需要使用 html 属性重载将 autocomplete="off" 添加到实际的 HTML 控件中。
或者当您在 asp.net 服务器控件中设置 autocomplete="off" 时呈现的实际 HTML 属性是什么。
编辑:影响所有文本框的一种选择是创建您自己的 Html 帮助器方法。只需创建一个扩展方法,如下所示:
然后您可以执行以下操作:
MVC does not have server controls like plain old ASP.NET. Therefore no server processing is done on your controls. They are rendered to the client exactly how you type them. Themes are not something you will use in MVC, because they apply to ASP.NET server controls and you won't be using those here. That said, HTML helpers do get processed by the server as the view is rendered. You will need to add autocomplete="off" to the actual HTML control using the html properties overload.
Or whatever the actual HTML attribute is that gets rendered when you set autocomplete="off" in the asp.net server control.
EDIT: One option to affect all text boxes would be to create your own Html helper method. Just create an extension method Like this:
Then you can just do:
为答案干杯,但是这些解决方案确实需要我编辑站点中的所有表单,如果您看到我们正在使用 MVC,您就会理解。
不管怎样,我选择了这种方式,在母版页中我添加了这个脚本:
我知道如果禁用了 javascript,这是毫无价值的,但是说实话,如果禁用了 javascript,用户将无法使用一半的网站。
Cheers for the answers guys, however those solutions would really require me to edit all the forms in the site, if you saw the use of MVC we are doing, you would understand.
Anyway i opted for this way, in the master page i added this script:
I know if javascript is disabled this is worthless however to be honest if javascript is disabled the users won't be able to use half the site.
AFAIK,您不能使用 css 执行
autocomplete = off
,它必须是 html 属性,因此您无法影响所有文本框。您可以做的一件事是将属性添加到表单中,如下所示(它将处理当前表单中的所有文本框)或创建一个类似于在内部添加此 html 属性的
BeginForm
的自定义 Helper 扩展方法。AFAIK, you cannot do
autocomplete = off
with css and it has to be a html attribute and hence there is nothing you can that would affect all Textboxes. One thing you can do is add the attribute to the form like this (it will address all textboxes in the current form)or create a custom Helper extension method that is similar to
BeginForm
that adds this html attribute internally.只需将其添加到 _Layout.cshtml 文档末尾
Simply add this to the _Layout.cshtml document end
而不是关闭写任何东西 @autocomplete='write-anything-instead-of-off'
Instead of off- write anything @autocomplete='write-anything-instead-of-off'
使用 autocomplete =“disable-autocomplete”
不再支持 autocomplete=“off”。
Use autocomplete = "disable-autocomplete"
The autocomplete= "off" is no longer supported.
使用以下 jQuery 代码
Use following jQuery Code