+-
c# – MVC HttpPost属性不起作用
出于某种原因,Get和Post都会触发第一个动作.

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

[HttpPost]
public ActionResult Login(FormCollection form)
{
   // Login Stuff here... never gets reached!
}

我基本上直接从MVC音乐商店样本中复制了这个.尝试在另一个应用程序,它工作正常.

这是一个相当新的项目,使用Visual Studio中的基本MVC3项目模板,所有默认设置.

我确保HTML输出指定POST方法:

<form action="/Home/Login" method="post">

这是我的Login.cshtml

@{
    ViewBag.PageTitle = "Login";
}
<section id="index">
<header>
    <h2>Login</h2>
</header>
<content>
    @using (Html.BeginForm("Login", "Home", FormMethod.Post))
    {
        <panel id="login">
            <table>
                <tr>
                    <td>Email:</td>
                    <td><input name="Email" /></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input name="Password" type="password" /></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input type="submit" value="Login" /></td>
                </tr>
            </table>
        </panel>
    }
</content>
</section>

提交表单后,我在浏览器中看到此URL:

http://localhost:51606/Home/[email protected]&Password=mypass

这些字段不应该在URL中!为什么我的表单会转换为GET请求?

最佳答案
再看一下HTML输出,我发现了我的表单周围的另一个表单标签.

原来有人(我)在Views / Shared / _Layout.cshtml中放置了一个表单标签,这是默认的共享布局.

呸,在这里输入问题后的数字我会发现问题.

点击查看更多相关文章

转载注明原文:c# – MVC HttpPost属性不起作用 - 乐贴网