10

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 &amp; Export" />
}

The generated HTML for this form has a blank action:

<form action="" method="post">  <input type="submit" value="Complete &amp; 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 }
        );
2
  • Can you show the registered routes? Commented Jun 16, 2011 at 0:13
  • I added the registered routes, but I'm confused by why that would matter since I can successfully do Html.BeginForm() Commented Jun 16, 2011 at 0:35

2 Answers 2

11

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.