为什么 Html.BeginForm 生成空操作?

发布于 2024-11-15 10:11:21 字数 1152 浏览 3 评论 0原文

我在一个名为 Admin 的区域中有一个控制器

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>

我想知道为什么它有一个空白操作?有关更多信息,我还添加了一个

@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 }
        );

I have a controller in an area called Admin

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

    public ViewResult CompleteAndExport() { ... }
}

and a view (ReadyForCompletion.cshtml) that has posts back to a different controller action on the same class

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

The generated HTML for this form has a blank action:

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

</form>

I want to know why this has a blank action? For more info, I also added in a

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

which also printed out an empty string. Also, if I use an empty Html.BeginForm() it generates the correct action.

Registered routes are

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贪了杯 2024-11-22 10:11:21

我相信您的问题是由连续的可选参数引起的。在我更改路线以包含两个可选参数之前,我无法复制您的问题。

请参阅:这个解释问题的文章

I believe your problem is caused by having consecutive optional parameters. I was not able to replicate your problem until I changed the route to contain two optional parameters.

See: This article which explains the problem

九歌凝 2024-11-22 10:11:21

对于那些使用 ASP.NET Core 遇到此问题的人来说,根本原因是相同的,但解决方案略有不同。我第一次在 Core 中看到这个,在调用 .MapRoutes() 时使用多个默认值。例如,

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

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

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

YMMV。

For those of you encountering this issue using ASP.NET Core the root cause is the same, though the solution is slightly different. I first saw this in Core using multiple default values when calling .MapRoutes(). E.g.

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

The workaround is to place the default values into the string template:

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

YMMV.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文