浏览器填充的字段超出了应有的范围

时间:2014-04-30 17:53:54

标签: c# asp.net-mvc-4

到目前为止,我在ASP.Net网站上有一个页面,其中包含2个部分视图,每个视图都有自己的表单,一个用于登录,另一个用于注册。

他们都工作得很好,除了一件事:如果我的浏览器在我登录时提示我记住我的密码,下次我尝试登录时,我在注册表单中的姓氏和密码将填充用户名来自登录表单的密码,例如下面的图片

Here is my problem

以下是登录和注册的模型

public class RegisterModel : IndexModel
{
    [Required]
    [Display(Name = "Email")]
    [EmailAddress]
    public string Email { get; set; }

    [Required]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Required]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

public class LoginModel : IndexModel
{
    [Required]
    [Display(Name = "Email")]
    [EmailAddress]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

登录部分

<% using (Html.BeginForm("Login", "Account", FormMethod.Post, new { id = "loginForm" }))
   { %>
    <%: Html.AntiForgeryToken() %>
    <%: Html.ValidationSummary(true) %>
        <div class="12u">
            <span class="input-with-caption fontsize-login">
                <label for="loginEmail"><asp:Literal runat="server" Text="<%$ Resources:LOGIN_EMAIL %>"></asp:Literal></label>
                <%: Html.TextBoxFor(m => m.Email, new {@class = "input-login-contacto redondo", id = "loginEmail"}) %>
            </span>
        </div>
        <div class="12u">
            <span class="input-with-caption fontsize-login">
                <label for="loginPassword"><asp:Literal runat="server" Text="<%$ Resources:LOGIN_PASSWORD %>"></asp:Literal></label>
                <%: Html.PasswordFor(m => m.Password, new {@class = "input-login-contacto redondo", id = "loginPassword"}) %>
            </span>
        </div>
        <div class="12u">
                <p class="remember"><%: Html.CheckBoxFor(m => m.RememberMe) %><asp:Literal runat="server" Text="<%$ Resources:LOGIN_REMEMBER %>"></asp:Literal></p>
        </div>

        <div class="row">
            <div class="5u">
                <input type="submit" name="login" value='<asp:Literal runat="server" Text="<%$ Resources:LOGIN_LOGINBTN %>"></asp:Literal>' class="submit-login-contacto fontsize-login redondo" />
            </div>
<% } %>     
             <%: Html.Action("ExternalLoginsList", "Account", new { ReturnUrl = ViewBag.ReturnUrl }) %>

这是注册部分

                 
    <div class="row">
        <div class="6u">
            <div class="12u">
                <span class="input-with-caption fontsize-login">
                    <label for="RegisterFirstName"><asp:Literal runat="server" Text="<%$ Resources:REGISTER_FIRSTNAME %>"></asp:Literal></label>
                    <%: Html.TextBoxFor(m => m.FirstName, new {id = "RegisterFirstName"}) %>
                </span>
            </div>
            <div class="12u">
                <span class="input-with-caption fontsize-login">
                    <label for="RegisterLastName"><asp:Literal runat="server" Text="<%$ Resources:REGISTER_LASTNAME %>"></asp:Literal></label>
                    <%: Html.TextBoxFor(m => m.LastName, new {id = "RegisterLastName"}) %>
                </span>
            </div>
            <div class="12u">
                <span class="input-with-caption fontsize-login">
                    <label for="RegisterPassword"><asp:Literal runat="server" Text="<%$ Resources:REGISTER_PASSWORD %>"></asp:Literal></label>
                    <%: Html.PasswordFor(m => m.Password, new {id = "RegisterPassword"}) %>
                </span>

            </div>
            <div class="12u">
                <span class="input-with-caption fontsize-login">
                    <label for="RegisterConfirmPassword"><asp:Literal runat="server" Text="<%$ Resources:REGISTER_CONFIRMPASS %>"></asp:Literal></label>
                    <%: Html.PasswordFor(m => m.ConfirmPassword, new {id = "RegisterConfirmPassword"}) %>
                </span>

            </div>
        </div>
        <div class="6u">
            <div class="row">
                <div class="12u">
                    <span class="input-with-caption fontsize-login">
                        <label for="RegisterEmail"><asp:Literal runat="server" Text="<%$ Resources:REGISTER_EMAIL %>"></asp:Literal></label>
                        <%: Html.TextBoxFor(m => m.Email, new {id = "RegisterEmail"}) %>
                    </span>
                </div>
                <div class="12u">
                    <%: Html.Captcha(5, "_CaptchaPartial") %>
                </div>

            </div>
        </div> 
    </div>
<% } %>

我已经更改了输入的ID和名称,但到目前为止还没有成功。 这是Chrome和Firefox上发生的问题(至少,这些是我尝试过的)。

我开始没有想法了。任何sugestions?

1 个答案:

答案 0 :(得分:1)

您可以使用autocomplete =“off”注释您的输入(您不希望浏览器记住的输入)

例如:<%: Html.PasswordFor(m => m.Password, new {id = "RegisterPassword", autocomplete="off"}) %>

http://css-tricks.com/snippets/html/autocomplete-off/

此外,浏览器将根据输入字段的名称记录输入字段中的值,然后有一组规则来应用它们(即使在不同的表单上),因此如果您在注册和登录时具有相同的字段名称表单浏览器可以自动填充它们:How to trigger Autofill in Google Chrome?