验证在加载视图之前发生

时间:2011-12-21 07:04:57

标签: jquery asp.net-mvc-2 unobtrusive-validation

好的,这是交易。我有一个简单的登录屏幕(在一个单独的区域),当我在菜单中选择登录时,登录屏幕加载完美,但它显示错误,就像加载视图执行验证(不显眼的jQuery)。 / p>

以下是登录页面的链接:

<li> <%: Html.ActionLink("Log In", "Index", "Login", new { area = "AdminArea" }, null)%></li>

这是登录视图:

<%= Html.RenderScript("MicrosoftMvcJQueryValidation")%>
<%= Html.RenderScript("json2")%>
<%= Html.RenderScript("loginForm")%>
<%  Html.EnableClientValidation(); %>

<% using (Html.BeginForm("Index", "Login", FormMethod.Post, new { @id = "signin" }))
{
    ViewContext.FormContext.ValidationSummaryId = "valLoginContainer"; %>
<fieldset>
<div class="row">
    <span class="label">
        <label for="Password">&nbsp;</label>
    </span>
    <table style="width" border="none;">
        <tr>
            <td>
                <%= Html.JQueryValidationSummary("Please fix the following error(s):", new Dictionary<string, object> { { "id", "valLoginContainer" } })%>
            </td>
        </tr>
    </table>
</div>
<legend>Admin Login</legend>
<div class="row">
    <span class="label">
        <label for="Username">Username:</label>
    </span>
    <%= Html.TextBoxFor(m=> m.Username, new { @class = "inputbox" })%><%= Html.ValidationMessageFor(m => m.Username, " *")%>
</div>
<div class="row">
    <span class="label">
        <label for="Password">Password:</label>
    </span>
    <%= Html.TextBoxFor(m=> m.Password, new { @class = "inputbox" })%><%= Html.ValidationMessageFor(m => m.Password, " *")%>
</div>
<div class="row">
    <span class="label">
        <label for="Password">&nbsp;</label>
    </span><a href="#" id="forgot_password_link" title="Click here to reset your password."> Forgot password?</a>
</div>
<div class="row">
    <span class="label">
        <label for="Password">&nbsp;</label>
    </span>
    <a href="#" id="forgot_username_link" title="Fogot your login name? We can help with that"> Forgot username?</a>
</div>
    <div class="row">
    <span class="label">
        <label for="Password">&nbsp;</label>
    </span>
    <input type="submit" id="action" value="Submit &raquo;" class="formButtons" />
</div>
</fieldset>
<%}%>

任何人都有任何想法为什么在视图加载时执行验证?

1 个答案:

答案 0 :(得分:0)

此问题已得到解决。我错过了一个JS文件(我不知道),一旦我添加了文件,一切都很美妙。另外,在LoginController.cs中我有这行不正确的

public virtual ActionResult Index()
{
    return View();
}

一旦我改变它,一切都很好'

public virtual ActionResult Index()
{
    return View(new LoginViewModel());
}

LoginViewModel如下所示:

public class LoginViewModel
{
    [Required(ErrorMessage = "Username/email is required")]
    public string Username { get; set; }

    [Required(ErrorMessage = "Password is required")]
    public string Password { get; set; }

    public bool RememberMe { get; set; }
}

感谢所有看过它的人。