gpt4 book ai didi

c# - MVC HttpPost 属性不起作用

转载 作者:太空狗 更新时间:2023-10-30 01:09:40 27 4
gpt4 key购买 nike

出于某种原因,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/Login?Email=me@email.com&Password=mypass

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

最佳答案

进一步查看 HTML 输出,我在我的表单周围发现了另一个表单标签。

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

呸,在这里输入问题后我会找到问题的数字。

关于c# - MVC HttpPost 属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6260394/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com