验证发生在视图加载之前

发布于 2024-12-22 12:05:05 字数 2595 浏览 5 评论 0原文

好吧,事情就是这样。我有一个简单的登录屏幕(在单独的区域中),当我在菜单中选择“登录”时,登录屏幕会完美加载,但它会显示错误,例如简单地加载视图即可执行验证(不显眼的 jQuery)。

这是登录页面的链接:

<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>
<%}%>

有人知道为什么在视图加载时执行验证吗?

Ok here's the deal. I have a simple log in screen (in a separate area), and when I select Log In in the menu the log in screen loads perfectly, but it shows the errors, like simply loading the view executes the validation (unobtrusive jQuery).

Here's the link for the Log In page:

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

This is the Login View:

<%= 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"> </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"> </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"> </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"> </label>
    </span>
    <input type="submit" id="action" value="Submit »" class="formButtons" />
</div>
</fieldset>
<%}%>

Anyone have any ideas why the validation is execute when the view loads?

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

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

发布评论

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

评论(1

-小熊_ 2024-12-29 12:05:05

这个问题已经解决了。我丢失了一个 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; }
}

感谢所有查看它的人。

This issue has been resolved. I was missing a JS file (that I was unaware of) and once I added the file everything works beautifully. Also, in LoginController.cs I had this line which was incorrect

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

Once I changed it to this all was well'

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

LoginViewModel looks like this:

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; }
}

Thanks to all who looked at it.

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