使用[AcceptVerbs(HttpVerbs.Post)]时未调用ActionResult

时间:2013-12-10 10:33:37

标签: asp.net-mvc c#-4.0 razor view controller

未调用控制器中的ActionResult。该页面只是令人耳目一新。 检查下面的代码:

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        @if (@ViewBag.Message != null)
        {
            <p style="color: #b94a48;">@ViewBag.Message</p>
        }    

        @Html.HiddenFor(model => model.Id)      

        <div class="form-group">

            <label class="control-label visible-ie8 visible-ie9">Username</label>
            <div class="input-icon">
                <i class="fa fa-user"></i>

                @Html.TextBoxFor(model => model.UserName, new { @placeholder = "Email", @class = "form-control placeholder-no-fix" })

                <p style="color: #b94a48;">@Html.ValidationMessageFor(model => model.UserName)</p>
            </div>
        </div>    

        <div class="form-group">
            <label class="control-label visible-ie8 visible-ie9">Password</label>
            <div class="input-icon">
                <i class="fa fa-lock"></i>    

                @Html.PasswordFor(model => model.Password, new { @placeholder = "Password", @class = "form-control placeholder-no-fix" })

                <p style="color: #b94a48;">@Html.ValidationMessageFor(model => model.Password)</p>
            </div>
        </div>
        <div class="form-actions">
            <div>
                <label class="checkbox">
                    @Html.CheckBoxFor(model => model.RememberMe, new { @class = "remember" }) Remember me
                </label>
            </div>
            <button type="submit" class="btn blue pull-right">
                Login <i class="m-icon-swapright m-icon-white"></i>
            </button>

        </div>

    </fieldset>
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Login(AccountViewModel model, FormCollection args, string ReturnUrl)
{
}

RouteConfig:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute("SJSS", "{controller}/Show/{assetCode}/{action}/{id}", new { controller = "Asset", action = "Index", id = UrlParameter.Optional });
        routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Account", action = "Home", id = UrlParameter.Optional });

3 个答案:

答案 0 :(得分:1)

尝试将@Html.BeginForm改为此:

@Html.BeginForm("***Action Name***", "***Controller Name***", FormMethod.Post)
{
}

答案 1 :(得分:0)

我可能会在这里弄错,但尽管您的身份确实有点令人困惑,但我找不到您要发布的网址,我的意思是,即使您提交,您的表单操作也是空的,怎么会mvc知道要调用哪个动作?

尝试在Html.BeginForm上设置动作和控制器参数。

编辑:检查@Roman Denysenko的答案,看看一个可能完全适合您案例的例子。

答案 2 :(得分:0)

您需要为表单指定“操作”,“控制器”和“表单方法”。请参阅以下代码:

@using (Html.BeginForm("Login", "Account", FormMethod.Post))
{
}