Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.BeginForm() with an absolute URL?

I need to have the POST action be to an absolute URL (e.g., http://www.cnn.com). Is there a way to use Html.BeginForm() helper and pass it the url?

like image 229
dale Avatar asked Dec 27 '09 03:12

dale


People also ask

What is HTML BeginForm ()?

"BeginForm()" is an extension method that writes an opening "<form>" tag to the response. "BeginForm()" is an extension method for both HtmlHelper and AjaxHelper classes.

What is the difference between Ajax BeginForm and HTML BeginForm?

Html. 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.

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. Here, is the method to create a form using Html.

Can we use multiple BeginForm in MVC?

Thanks for your help ! Multiple form tags should work fine in MVC unless they are nested.


1 Answers

BeginForm method has several overloads. In order to set the action attribute on the form tag with the desired url, you need to use following overload of BeginForm:

BeginForm(String, String, FormMethod, IDictionary<String, Object>) // here are the parameter names: BeginForm(actionName, controllerName, method, htmlAttributes) 

Since you want to post to an external site, there is no need to set actionName and controllerName, just leave them as null.

@Html.BeginForm(    null, null, FormMethod.Post, new {@action="http://cnn.com/post"} ) 

This will not encode the action parameter.

like image 93
Nikita Ignatov Avatar answered Oct 02 '22 10:10

Nikita Ignatov