禁用 MVC 中 html 帮助文本框的自动完成功能

发布于 2024-10-21 07:31:26 字数 337 浏览 1 评论 0原文

好的,

我会在正常的 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 技术交流群。

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

发布评论

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

评论(9

云裳 2024-10-28 07:31:26

MVC 没有像普通的旧 ASP.NET 那样的服务器控件。因此,您的控件上不会进行任何服务器处理。它们将按照您键入的方式呈现给客户端。主题不是您将在 MVC 中使用的内容,因为它们适用于 ASP.NET 服务器控件,并且您不会在此处使用它们。也就是说,HTML 助手确实会在视图呈现时由服务器进行处理。您需要使用 html 属性重载将 autocomplete="off" 添加到实际的 HTML 控件中。

@Html.TextBoxFor(x => x.Something, new { autocomplete="off" } )

或者当您在 asp.net 服务器控件中设置 autocomplete="off" 时呈现的实际 HTML 属性是什么。

编辑:影响所有文本框的一种选择是创建您自己的 Html 帮助器方法。只需创建一个扩展方法,如下所示:

using System.Web.Mvc;
using System.Web.Mvc.Html;

public static MvcHtmlString NoAutoCompleteTextBoxFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
    return html.TextBoxFor(expression, new { autocomplete="off" });
}

然后您可以执行以下操作:

Html.NoAutoCompleteTextBoxFor(x => x.Something)

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.

@Html.TextBoxFor(x => x.Something, new { autocomplete="off" } )

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:

using System.Web.Mvc;
using System.Web.Mvc.Html;

public static MvcHtmlString NoAutoCompleteTextBoxFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
    return html.TextBoxFor(expression, new { autocomplete="off" });
}

Then you can just do:

Html.NoAutoCompleteTextBoxFor(x => x.Something)
時窥 2024-10-28 07:31:26

为答案干杯,但是这些解决方案确实需要我编辑站点中的所有表单,如果您看到我们正在使用 MVC,您就会理解。

不管怎样,我选择了这种方式,在母版页中我添加了这个脚本:

<script language="javascript" type="text/javascript">
        $(document).ready(function () {
            try {
                $("input[type='text']").each(function(){
                               $(this).attr("autocomplete","off");
                            });
            }
            catch (e)
            { }
        });

    </script>

我知道如果禁用了 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:

<script language="javascript" type="text/javascript">
        $(document).ready(function () {
            try {
                $("input[type='text']").each(function(){
                               $(this).attr("autocomplete","off");
                            });
            }
            catch (e)
            { }
        });

    </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.

追我者格杀勿论 2024-10-28 07:31:26

AFAIK,您不能使用 css 执行 autocomplete = off ,它必须是 html 属性,因此您无法影响所有文本框。您可以做的一件事是将属性添加到表单中,如下所示(它将处理当前表单中的所有文本框)

Html.BeginForm(action,controller, FormMethod.Post, new { autocomplete="off"})

或创建一个类似于在内部添加此 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)

Html.BeginForm(action,controller, FormMethod.Post, new { autocomplete="off"})

or create a custom Helper extension method that is similar to BeginForm that adds this html attribute internally.

苍景流年 2024-10-28 07:31:26

只需将其添加到 _Layout.cshtml 文档末尾

<script>
    $("input[type='text']").attr("autocomplete","off");
</script>

Simply add this to the _Layout.cshtml document end

<script>
    $("input[type='text']").attr("autocomplete","off");
</script>
南汐寒笙箫 2024-10-28 07:31:26

而不是关闭写任何东西 @autocomplete='write-anything-instead-of-off'

@Html.TextBoxFor(model => model.FirstName, new { placeholder = "First Name", @class = "form-control", @id = "txtFirstName", @autocomplete = "new-FirstName"})

Instead of off- write anything @autocomplete='write-anything-instead-of-off'

@Html.TextBoxFor(model => model.FirstName, new { placeholder = "First Name", @class = "form-control", @id = "txtFirstName", @autocomplete = "new-FirstName"})
安人多梦 2024-10-28 07:31:26
@Html.TextBox("Address","",new { @class = "classname", autocomplete = "off" })
@Html.TextBox("Address","",new { @class = "classname", autocomplete = "off" })
々眼睛长脚气 2024-10-28 07:31:26

使用 autocomplete =“disable-autocomplete”

不再支持 autocomplete=“off”。

Use autocomplete = "disable-autocomplete"

The autocomplete= "off" is no longer supported.

那请放手 2024-10-28 07:31:26

ASP.net MVC HTML 属性:具有 type="text"
由JQUERY代码管理

@Html.TextBoxFor(model => model.FirstName, new {@id = "txtFirstName"})


<script language="javascript" type="text/javascript">
        $(document).ready(function () {
            try {
                $("input[type='text']").each(function () {
                    $(this).attr("autocomplete", "off-1");
                });
            }
            catch (e) { }
        });
    
    </script>

ASP.net MVC HTML Attribute: Which is having type="text"
Managed by JQUERY code

@Html.TextBoxFor(model => model.FirstName, new {@id = "txtFirstName"})


<script language="javascript" type="text/javascript">
        $(document).ready(function () {
            try {
                $("input[type='text']").each(function () {
                    $(this).attr("autocomplete", "off-1");
                });
            }
            catch (e) { }
        });
    
    </script>
豆芽 2024-10-28 07:31:26

使用以下 jQuery 代码

 $(document).ready(function() {
    $("form,input").attr("autocomplete","off");
 });

Use following jQuery Code

 $(document).ready(function() {
    $("form,input").attr("autocomplete","off");
 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文