Search code examples
asp.net-mvc-3html.beginform

Why does Html.BeginForm generate empty action?


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

Solution

  • 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