Forms in MVC:-
  1. It is important for the developer to learn about “form” tag. The “form” tag is very powerful and without “form” tag, internet would be read-only document repository.
  2. Many developers coming to the MVC framework is the webform developers. WebForms does not completely expose the full power of “Form” tag.
  3. A form is a container for input elements like textbox, button, dropdown list, checkbox etc. These input elements allow the user to enter the information and submit the information to the server. How does information get into the server?
  4. “Form” tag has the information about the server. Form tag hold two attribute “action” and “method”.
  5. Action:- This attribute tell the browser where to send the information
  6. Method:- this attribute tell the browser which method should be used to send the information (GET or POST)


Example:-

  

<form action="/Home/Search" method="get">
  <input type="text" name="q" />
  <input type="submit" value="Search" />
</form>



  1. By default “Form” tag uses the “GET” method to send the information to the server. Web application generally use the GET request for Read operation and POST for write operation.
  2. GET:- this method will not change state on the server. All the parameters are passed through url
  3. POST:- this method will change the server state. All the parameters are passed through the request.


Form tag in MVC:-

 Form tags can be added to the views in MVC application using HTML helper. Below example show the adding the form tag in view

 
@using (Html.BeginForm("Search", "Home", FormMethod.Get)) {
<input type="text" name="q" />
<input type="submit" value="Search" />
}



Other way to represent the form:-

  

            
@{Html.BeginForm("Search", "Home", FormMethod.Get);}
<input type="text" name="q" />
<input type="submit" value="Search" />
@{Html.EndForm();}




Beginform method in the HTML helper expose the method with following syntax, where you can mention the controller name and action method for navigation.

  

HTML.BeginForm([Action method name], [Controller name], form method)




Html.BeginForm also provides no of overloaded method for specifying the target, data validation and class.

  


@using (Html.BeginForm("Search", "Home", FormMethod.Get,
new { target = "_blank", @class="editForm", data_validatable=true }))



This is equal to:-

  

<form action="/Home/Search" class="editForm" data-validatable="true"
method="get" target="_blank">