Submit (Post) Form Using ActionLink in ASP - Net MVC
Submit (Post) Form Using ActionLink in ASP - Net MVC
Submit (Post) Form Using ActionLink in ASP - Net MVC
aspx
Hence in order to submit (post) Form using @Html.ActionLink, a jQuery Click event handler is
assigned and when the @Html.ActionLink is clicked, Form is submitted (posted) using JavaScript
Submit function.
In this article I will explain with an example, how to submit (post) Form using ActionLink in ASP.Net MVC 5
Razor.
@Html.ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request
to the Controller’s Action method which cannot be used to submit (post) Form in ASP.Net MVC 5 Razor.
Hence in order to submit (post) Form using @Html.ActionLink, a jQuery Click event handler is assigned
and when the @Html.ActionLink is clicked, Form is submitted (posted) using JavaScript Submit function.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with
Sample Program example.
Model
Following is a Model class named PersonModel with four properties i.e. PersonId, Name, Gender and
City.
public class PersonModel
///<summary>
///</summary>
///<summary>
///</summary>
///</summary>
///<summary>
///</summary>
Controller
Then you will need to add a Controller class to your project. There are two Action methods with the name
Index, one for handling the GET operation while other for handling the POST operation.
The Action method for POST operation accepts an object of the PersonModel class as parameter. The
values posted from the Form inside the View are received through this parameter.
public class HomeController : Controller
// GET: Home
return View();
[HttpPost]
View
Inside the View, in the very first line the PersonModel class is declared as Model for the View.
The View consists of an HTML Form which has been created using the Html.BeginForm method with the
following parameters.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
There are three TextBox fields created for capturing values for PersonId, Name and City using the
Html.TextBoxFor method. While for capturing the Gender value, a DropDownList with three options is
created using the Html.DropDownListFor function.
There’s also a Submit ActionLink at the end of the Form generated using @Html.ActionLink method and
when the ActionLink is clicked, the Form is submitted using jQuery.
@model ActionLink_Send_Model_MVC.Models.PersonModel
@{
Layout = null;
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<tr>
</tr>
<tr>
<td>PersonId: </td>
<td>
</td>
</tr>
<tr>
<td>Name: </td>
<td>
</td>
</tr>
<tr>
<td>Gender: </td>
<td>
</td>
</tr>
<tr>
<td>City: </td>
<td>
</td>
</tr>
<tr>
<td></td>
<td>@Html.ActionLink("Submit", "", null, new { @id = "submit"
})</td>
</tr>
</table>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#submit").click(function () {
document.forms[0].submit();
return false;
});
});
</script>
</body>
</html>
Screenshots
The Form