2014-10-27 93 views
2

我有这段代码;MVC5表格没有正确路由

<form action="@Url.Action("Index", "MyController")" method="post" >    
    <button class="btn btn-large btn-primary" id="btnNext">Click Here</button> 
</form> 

但按钮被点击时,它重定向到http://<domain>:2024/MyController,我得到一个HTTP错误

405.0目录列表是不允许的。

如果我手动更改url为http://<domain>:2024/MyController/Index我得到我的页面。

当我将表单代码更改为下面的工作。

<form action="@Url.Action("MyAction", "MyController")" method="post" >    
    ... 

索引方法声明是;

public async Task<ActionResult> Index() 
{ 
    ... 
} 

所以这是一个不便,这是它发生的唯一的地方。我可以通过不使用索引操作来解决它,但我怎样才能跟踪并修复它?

+0

您可以在MyController中使用HttpPost属性来共享您的Index操作的声明吗? – 2014-10-27 11:52:27

+0

看看http://serverfault.com/a/405585/56843有帮助。 – GSerg 2014-10-27 11:53:43

+0

@GSerg该链接是指403错误,而不是405? – DavidG 2014-10-27 11:54:43

回答

0

除了检查您的路由规则,您还可以使用Html.BeginForm辅助方法正确地创建表单。

@using(Html.BeginForm("Action", "ControllerName"), HttpMethod.Post) 
{ 
//form controls 
}