使用MVC 4提交表单不会触发我在BeginForm中指定的Action

时间:2014-09-10 12:36:25

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

我有问题将表单提交到Controller中的特定方法。

当用户点击我的提交按钮时,会触发Index操作,忽略我在ValidateLogin

指定的BeginForm操作

我的表格有什么问题?

HTML

@model Login.Models.Autenticacao
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link href="~/Content/bootstrap.css" rel="stylesheet" />
    <link href="~/Content/endless.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-1.8.2.js"></script>    
</head>
    <body>
            <div id="formAutenticacao">
                <div class="pace pace-inactive">
                    <div data-progress="99" data-progress-text="100%" style="width: 100%;" class="pace-progress">
                        <div class="pace-progress-inner"></div>
                    </div>
                    <div class="pace-activity"></div>
                </div>
                <div class="login-wrapper">
                    <div class="text-center">
                        <h2 class="fadeInUp animation-delay8" style="font-weight: bold">
                            <img src="~/Images/Horizonta_com_S.png" />
                        </h2>
                    </div>
                    <div class="login-widget animation-delay1">
                        <div class="panel panel-default">
                            <div class="panel-body">
                                    @using (Html.BeginForm("ValidateLogin", "Login", FormMethod.Post))
                                    {
                                        <div class="form-group">
                                            <label>User</label>
                                            @Html.TextBoxFor(m => m.UserName, new {placeholder = "Type user name", Class = "form-control input-sm bounceIn animation-delay2"})
                                        </div>
                                        <div class="form-group">
                                            <label>Password</label>
                                            @Html.PasswordFor(m => m.Password, new {placeholder = "Type the password", Class = "form-control input-sm bounceIn animation-delay4"})
                                        </div>
                                        <div class="seperator"></div>
                                        <hr>

                                        <input type="submit" value="Go!" />
                                    }
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <iframe id="iframePrincipal" height="0" width="100%" frameborder="0"></iframe>
</body>
</html>

登录控制器

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

    [HttpPost]
    public ActionResult ValidateLogin(Autenticacao form)
    {
        return View();
    }

路线

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
        );
    }

模型

public class Autenticacao
{
    [Required]
    public virtual string UserName { get; set; }

    [Required]
    public virtual string Password { get; set; }
}

萤火虫

仅供参考:我在此问题中将Action的名称翻译为更好的理解。

enter image description here

1 个答案:

答案 0 :(得分:1)

发现我的错误。

在Web.config中,我评论以下行

<authorization> <deny users="?" /> </authorization>