Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Html.BeginForm and specifying action

Tags:

asp.net-mvc

I have been trying to do the following but whatever I try I just keep getting different errors:

@using (Html.BeginForm(Url.Action(this.ViewContext.RouteData.Values["action"] as string)))

This for example produces:

<form action="/adminTests/create?Length=22" method="post">

Has anyone figured out how to do this?

like image 814
Marcel Avatar asked May 16 '11 16:05

Marcel


People also ask

What is HTML BeginForm ()?

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. Here, is the method to create a form using Html. BeginForm extension method in ASP.NET MVC5. BeginForm("ActionMethod", "ControllerName","Get⁄Post Method")

What is difference between HTML BeginForm and Ajax BeginForm?

BeginForm() will create a form on the page that submits its values to the server as a synchronous HTTP request, refreshing the entire page in the process. Ajax. BeginForm() creates a form that submits its values using an asynchronous ajax request.

Which two objects can you passed as a parameter to the HTML helper Dot BeginForm method?

In the "BeginForm()" method we passed an action name and controller name so that the action method will be called that handles the post data request.


1 Answers

You are using a wrong overload. It should be:

@using (Html.BeginForm(new { action = ViewContext.RouteData.Values.GetRequiredString("action") }))

or:

@using (Html.BeginForm(ViewContext.RouteData.Values.GetRequiredString("action"), ViewContext.RouteData.Values.GetRequiredString("controller"))))

or if you want to generate a form POSTing to the current url use simply (note that this will include any query string parameters):

@using (Html.BeginForm())

For full list of available overloads consult the documentation.

like image 101
Darin Dimitrov Avatar answered Oct 30 '22 01:10

Darin Dimitrov