【问题标题】:Allow validation for only one of the button in MVC仅允许验证 MVC 中的一个按钮
【发布时间】:2014-10-12 11:25:33
【问题描述】:

我使用 MVC Razor 创建了一个登录页面。该页面包含要输入的用户名和密码以及两个按钮登录和注册按钮。

我已经为两个文本框应用了必填字段验证。当我单击登录按钮时,它工作正常。对于注册按钮,我希望将页面重定向到注册页面。但是当我点击注册按钮时,它会抛出错误,因为需要用户名和密码。

仅单击登录按钮时如何检查验证。

查看页面:

<body>
    @using (Html.BeginForm("CheckLogin", "Login"))
    {
        <div>
            <div>
                <img src="@Url.Content("../../Content/Images/Logo.png")" alt="logo" style="margin-top:17px;" />@*width="184" height="171"*@
            </div>
            <div class="LoginBg">
                <h3 class="LoginHdng">
                    @Html.Label("lblLogin", "Login")
                </h3>
                <table style="width: 745px;">
                    <tr>
                        <td>
                            <br />
                        </td>
                    </tr>
                    <tr>
                        <td style="font-size: 20px; text-align: right; width: 250px">
                            @Html.LabelFor(model => model.FirstName, "User Name: ")
                        </td>
                        <td>
                            @Html.TextBoxFor(model => model.FirstName, new { @class = "txtBox" })
                            @Html.ValidationMessageFor(model => model.FirstName)
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <br />
                        </td>
                    </tr>
                    <tr>
                        <td style="font-size: 20px; text-align: right;">
                            @Html.LabelFor(model => model.Password, "Password: ")
                        </td>
                        <td>
                            @Html.PasswordFor(model => model.Password, new { @class = "txtBox" })
                            @Html.ValidationMessageFor(model => model.Password)
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" style="font-size: 16px; text-align: center; color: Red;">
                            <br />
                            @TempData["Error"]
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <br />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" style="text-align: center;">
                            <input type="submit" value="Login" name="Command" class="loginBtn" />
                            &nbsp;&nbsp;&nbsp;&nbsp; 
                            <input type="submit" value="SignUp" name="Command" class="loginBtn" />
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    }
</body>

控制器:

        [HttpPost]
        public ActionResult CheckLogin(FormCollection coll)//, string command
        {
            if (coll["Command"] == "Login")
            {
                using (var db = new MVC_DBEntities())
                {
                    if (db.tbl_UserDetails.ToList().Select(m => m.FirstName == coll["FirstName"] && m.Password == coll["Password"]).FirstOrDefault())
                    {
                        TempData["Error"] = "";
                        return this.RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        TempData["Error"] = "Invalid UserName/Password";
                        return this.RedirectToAction("Login", "Login");
                    }
                }
            }
            else if (coll["Command"] == "SignUp")
            {
                return this.RedirectToAction("SignUp", "SignUp");
            }
            else
            {
                return this.RedirectToAction("Login", "Login");
            }
        }

在此先感谢...

【问题讨论】:

  • 尝试将调试点放在 else if (coll["Command"] == "SignUp") 并检查其到达是否听到?

标签: jquery asp.net asp.net-mvc asp.net-mvc-3 razor


【解决方案1】:

您可以简单地为不需要验证的按钮添加一个取消类,如下所示:

<input type="submit" value="SignUp" name="Command" class="loginBtn cancel" />

这将绕过验证,前提是您使用的是内置的 jQuery 非侵入式验证。

【讨论】:

    【解决方案2】:

    Signup按钮作为输入类型按钮而不是输入类型提交如图:-

    <input type="button" value="SignUp" name="Command" class="loginBtn" id="SignUpBtn" />
    

    并使用一些 Jquery/JavascriptSignUp Page as :-

    $("#SignUpBtn").click(function(){
       window.location.href="/SignUp/SignUp";
    });
    

    【讨论】:

    • 感谢您的回复。我已将 SignUp 按钮的类型更改为 button 。现在按钮单击什么也不做。我是否必须为此按钮添加单独的控制器功能。
    • 非常感谢..一个简单而完美的答案,适合我的需要..再次感谢...
    【解决方案3】:

    有几种方法。

    1. 你不能把“注册”作为链接,并使用 css 显示为按钮。
    2. 将“SingUp”按钮包裹在单独的表单周围
    1. 或者按照建议使用带有 value 属性的按钮。您可以使用 js 重定向或在控制器中您可以查找值和重定向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      • 2018-09-08
      • 2014-01-04
      相关资源
      最近更新 更多