Introduction
- How to add jquery in project
- Where NuGet stores installed Packages details
- Jquery Ajax Form Submit using Serializing Form Data into a Model

Create a project called “AspNetMvcJqueryAjaxSerializeForm”
Click Ok to proceed
By default view after “AspNetMvcJqueryAjaxSerializeForm”project created.
In empty project template visual studio will not give any controller , model and view. All three folders called CONTROLLERS, MODELS and VIEWS are blank only Web.Config file exists inside VIEWS folder.
Now we are going to add Jquery into project with help of NUGET.
Right Click on Project title inside SOLULTION EXPLORER.
How to add jquery in project?
Select option called Manage NuGet Packages. . .
Follow the simple steps as given in image
STEPS
- Select browse to do search for jquery.
- Type “Jquery” to search.
- Single click on Jquery
- You can select your desired version.
- Click on Install
In the above you can see jquery installed successfully. You can even see Package.config file.
Where NuGet store installed Packages details?


- Index.
- Details
- Create -- HTTP GET
- Create - HTTP POST
- Edit -- HTTP GET
- Edit - HTTP POST
- Delete - HTTP GET
- Delete - HTTP POST
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace AspNetMvcJqueryAjaxSerializeForm.Models
- {
- public class FriendModel
- {
- public string FriendName { get; set; }
- public string Phone { get; set; }
- public string State { get; set; }
- }
- }
At left side bottom you can see “Build Succeeded”.
Right click on Create ActionResult.
Now you can see in Shared folder ---> _Layout.cshtml created.
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>

Copy this link and paste at head section, we had added latest version of jquery and you can cross check in SCRIPTS folder the jquery version. Change according current version you had downloaded from NuGet.
Move the this line <script src="~/Scripts/jquery-3.4.1.min.js"></script> Code at top following location:
To check if jquery has started working or not you just put the following code at the bottom of Create.cshtml.
Press F5 and execute project you will get following screen:
- <script>
- $(document).ready(function(){
- console.log("ready!");
- alert("ready");
- });
- </script>
You can see tin he above screenshot the ready alert box has appeared.
Now switch back to FriendController
Add namespace at top of the file
- // POST: Friend/AddFriend
- [HttpPost]
- public ActionResult AddFriend(FriendModel fm)
- {
- //Write your database insert code / activities
- return RedirectToAction("create");
- }
- @{
- ViewBag.Title = "Create";
- }
- <h2>Create</h2>
- <form id="friendform">
- <table>
- <tr>
- <td>Friend Name</td>
- <td><input id="txtFriendName" name="FriendName" type="text" /></td>
- </tr>
- <tr>
- <td>Phone</td>
- <td><input id="txtPhone" name="Phone" type="text" /></td>
- </tr>
- <tr>
- <td>State</td>
- <td><input id="txtState" name="State" type="text" /></td>
- </tr>
- </table>
- <input id="btnsubmit" type="button" value="Submit"/>
- </form>
- <script>
- $(document).ready(function(){
- console.log("ready!");
- alert("ready");
- });
- </script>
Our next step is to insert Jquery Ajax Form Submit with Serializing because our article title is Asp.Net MVC jQuery AJAX Form Submit using Serialize Form Data into Model.
Switch back to CREATE.CSHTML file to insert AJAX code.
- <script>
- $(document).ready(function () {
- $("#btnsubmit").click(function (e) {
- //Serialize the form datas.
- var valdata = $("#friendform").serialize();
- //to get alert popup
- alert(valdata);
- $.ajax({
- url: "/Friend/AddFriend",
- type: "POST",
- dataType: 'json',
- contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
- data: valdata
- });
- });
- });
- </script>
You can see in the above form where we enter data. On clicking on SUBMIT button this data we should get in controller side ADDFRIEND method.
As I clicked on SUBMIT button alert popup appeared.
You can see values are separated with & symbol.
In the above screen you can see we received data from view. To recap the article, we have taken following steps:
- Created Empty Asp.Net MVC project
- Installed Jquery from NuGet
- Created Controller called “FriendController”
- Created View “Create”
- Created ActionMethod “AddFriend”
FriendController.cs Code
FriendModel.cs Code
Create.cshtml Code
_Layout.cshtml Code
Happy Coding.
- using AspNetMvcJqueryAjaxSerializeForm.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace AspNetMvcJqueryAjaxSerializeForm.Controllers
- {
- public class FriendController : Controller
- {
- // GET: Friend/Create
- public ActionResult Create()
- {
- return View();
- }
- // POST: Friend/AddFriend
- [HttpPost]
- public ActionResult AddFriend(FriendModel fm)
- {
- //Write your database insert code / activities
- return RedirectToAction("create");
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace AspNetMvcJqueryAjaxSerializeForm.Models
- {
- public class FriendModel
- {
- public string FriendName { get; set; }
- public string Phone { get; set; }
- public string State { get; set; }
- }
- }
- @{
- ViewBag.Title = "Create";
- }
- <h2>Create</h2>
- <form id="friendform">
- <table>
- <tr>
- <td>Friend Name</td>
- <td><input id="txtFriendName" name="FriendName" type="text" /></td>
- </tr>
- <tr>
- <td>Phone</td>
- <td><input id="txtPhone" name="Phone" type="text" /></td>
- </tr>
- <tr>
- <td>State</td>
- <td><input id="txtState" name="State" type="text" /></td>
- </tr>
- </table>
- <input id="btnsubmit" type="button" value="Submit" />
- </form>
- <script>
- $(document).ready(function () {
- $("#btnsubmit").click(function (e) {
- //Serialize the form datas.
- var valdata = $("#friendform").serialize();
- //to get alert popup
- alert(valdata);
- $.ajax({
- url: "/Friend/AddFriend",
- type: "POST",
- dataType: 'json',
- contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
- data: valdata
- });
- });
- });
- </script>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>@ViewBag.Title - My ASP.NET Application</title>
- <link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
- <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
- <script src="~/Scripts/modernizr-2.6.2.js"></script>
- <script src="~/Scripts/jquery-3.4.1.min.js"></script>
- </head>
- <body>
- <div class="navbar navbar-inverse navbar-fixed-top">
- <div class="container">
- <div class="navbar-header">
- <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
- </div>
- <div class="navbar-collapse collapse">
- <ul class="nav navbar-nav">
- </ul>
- </div>
- </div>
- </div>
- <div class="container body-content">
- @RenderBody()
- <hr />
- <footer>
- <p>© @DateTime.Now.Year - My ASP.NET Application</p>
- </footer>
- </div>
- <script src="~/Scripts/bootstrap.min.js"></script>
- </body>
- </html>

German MedinaPosted May 27, 2022, 7:02 PM
Very good article, very useful! Thank you so much
Daniel VegaPosted Dec 18, 2021, 5:45 PM
I love, thanks
Mageshwaran RPosted Nov 15, 2019, 12:17 AM
Super Article...
Sourav Kumar DasPosted Nov 6, 2019, 10:37 PM
Nice Article Sir...