MVC HttpPost属性不工作 [英] MVC HttpPost attribute not working
问题描述
由于某种原因同时获取和火灾后的第一个动作。
For some reason both the Get and Post fire the first action.
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(FormCollection form)
{
// Login Stuff here... never gets reached!
}
我基本上都是直接从MVC音乐商店样品复制这一点。试图在另一个应用程序,它能正常工作。
I've basically copied this directly from the MVC music store sample. Tried it in another app and it worked fine.
这是一个相当新的项目,使用Visual Studio中的基础MVC3项目模板,所有默认设置。
this is a fairly new project, used the base MVC3 project template in Visual Studio, all the default settings.
我确定HTML输出指定POST方法:
I made sure the HTML output specifies the POST method:
<form action="/Home/Login" method="post">
下面是我的Login.cshtml
Here is my 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>
在我提交我看到这个网址在浏览器的形式为:
After I submit the form I see this URL in my browser:
的http://本地主机:51606 /主页/登录? Email=me@email.com&密码=为mypass
这些字段不应该在URL!为什么地球上是我的形式越来越转换为GET请求?
Those fields should not be in the URL! Why on earth is my form getting converted to a GET request?
推荐答案
纵观HTML输出更多一些,我发现另一种形式的标签我周围的形式。
Looking at the HTML output some more, I spotted another form tag surrounding my form.
原来有人(我)把一个表单标签中查看/共享/ _Layout.cshtml,这是默认共享的布局。
Turns out someone (me) put a form tag in the Views/Shared/_Layout.cshtml, which is the default shared layout.
呸,人物的问题键入后在这里我会发现这个问题。
bah, figures after typing in the question here I would find the problem.
这篇关于MVC HttpPost属性不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!