2011-06-16 119 views
9

我的区域中的控制器称为Admin为什么Html.BeginForm会生成空行为?

public class SiteVisitController : Controller 
{ 
    public ViewResult ReadyForCompletion() { ... } 

    public ViewResult CompleteAndExport() { ... } 
} 

并且具有回发到在同一类中的不同控制器操作的图(ReadyForCompletion.cshtml

@using (Html.BeginForm("CompleteAndExport", "SiteVisit")) 
{   
    <input type="submit" value="Complete &amp; Export" /> 
} 

这种形式生成的HTML有一个空白的动作:

<form action="" method="post"> <input type="submit" value="Complete &amp; Export" /> 

</form> 

我想知道为什么这有ab行动?对于更多的信息,我还添加在

@Url.RouteUrl(new { controller = "ReadyForCompletion", action = "SiteVisit", area = "Admin" }) 

其也打印出来一个空字符串。另外,如果我使用空的Html.BeginForm()它会生成正确的操作。

注册的路线是

 context.MapRoute(
      "Admin_manyParams", 
      "Admin/{controller}/{action}/{id}/{actionId}", 
      new { action = "Index", id = UrlParameter.Optional, actionId = UrlParameter.Optional } 
     ); 
+0

你能显示注册路线吗? – 2011-06-16 00:13:34

+0

我添加了注册的路线,但我很困惑,为什么这很重要,因为我可以成功地做'Html.BeginForm()' – kelloti 2011-06-16 00:35:26

回答

10

我相信你的问题是由具有连续的可选参数引起的。直到我将路线更改为包含两个可选参数之前,我无法复制您的问题。

参见:This article which explains the problem

+0

您是正确的,先生。疯! – kelloti 2011-06-16 01:31:23

0

对于那些你遇到使用ASP.NET核心的根本原因是一样的,但解决的办法是稍有不同这个问题。我在调用.MapRoutes()时,首先在Core中使用多个默认值来看到这一点。例如。

routes.MapRoute(
    name: "default", 
    template: "{controller}/{action}/{id?}", 
    defaults: new { controller = "Foo", action = "Bar" } 
); 

的解决方法是将默认值放入字符串模板:

routes.MapRoute(
    name: "default", 
    template: "{controller=Foo}/{action=Bar}/{id?}" 
); 

情况因人而异。

相关问题