Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC HttpPost属性不工作_C#_Asp.net Mvc_Asp.net Mvc 3_Razor - Fatal编程技术网

C# MVC HttpPost属性不工作

C# MVC HttpPost属性不工作,c#,asp.net-mvc,asp.net-mvc-3,razor,C#,Asp.net Mvc,Asp.net Mvc 3,Razor,出于某种原因,Get和Post-fire都是第一个操作 public ActionResult Login() { return View(); } [HttpPost] public ActionResult Login(FormCollection form) { // Login Stuff here... never gets reached! } 我基本上是直接从MVC音乐商店示例中复制的。在另一个应用程序中试用,效果很好 这是一个相当新的项目,使用VisualStud

出于某种原因,Get和Post-fire都是第一个操作

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

[HttpPost]
public ActionResult Login(FormCollection form)
{
   // Login Stuff here... never gets reached!
}
我基本上是直接从MVC音乐商店示例中复制的。在另一个应用程序中试用,效果很好

这是一个相当新的项目,使用VisualStudio中的基本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>
@{
ViewBag.PageTitle=“登录”;
}
登录
@使用(Html.BeginForm(“Login”、“Home”、FormMethod.Post))
{
电邮:
密码:
}
提交表单后,我在浏览器中看到此URL:


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

再看一下HTML输出,我发现表单周围还有另一个表单标签

结果发现有人(我)在Views/Shared/_Layout.cshtml中放置了一个表单标记,这是默认的共享布局

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

我刚刚在表单标签中添加了method=“post”action=“”,效果很好

@{
    ViewBag.Title = "Add New Entry";
}

<h2>Add New Entry</h2>


<form method="post" action="">  

       <fieldset>

            Please enter your name: <br />
            <input type="text" name="Name" maxlength="200" />
            <br /><br />
            Please enter your message: <br />
            <textarea name="Message" rows="10" cols="40"> </textarea>
             <br /><br />
            <input type="submit" value="Submit Entry" /> 
        </fieldset>
</form>       
@{
ViewBag.Title=“添加新条目”;
}
添加新条目
请输入您的姓名:


请输入您的消息:



我复制了您的代码,但对我来说效果很好。对不起,我甚至不知道该建议什么!大概确保使用了正确的视图???@Beno:请参阅下面我的答案。我应该先查看整个HTML输出。