Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 }
        );
like image 487
kelloti Avatar asked Jun 16 '11 00:06

kelloti


People also ask

How does HTML BeginForm work?

BeginForm(HtmlHelper, String, String, FormMethod, Object)Writes an opening <form> tag to the response and sets the action tag to the specified controller and action. The form uses the specified HTTP method and includes the HTML attributes.

What is difference between HTML BeginForm and Ajax BeginForm?

@Html. BeginForm is used to post the data by full page refresh whereas @Ajax. BeginForm performs Post back function and allows some portion of Html to be reloaded rather than overall page refresh.

Why we use HTML BeginForm in MVC?

BeginForm helper method contains a couple overloads whose intended purpose is to make writing routed forms easier. It is aware of MVC stucture and makes sure its targeting a controller and action. It's just a bit of syntactic sugar over: <form method="post" action="@Url.

Why we use HTML BeginForm?

Html. BeginForm is the Html Helper Extension Method that is used for creating and rendering the form in HTML. This method makes your job easier in creating form.


1 Answers

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

like image 136
user122211 Avatar answered Nov 03 '22 21:11

user122211