Insert data using jQuery AJAX in ASP.Net MVC Razor

   11/5/2021 2:58:18 PM     ASP.Net MVC jQuery AJAX Insert data Add Data Submit Data     0

Share us on:

Linkedin GooglePlus

Hi, i am Shubham Kumar, I am going to explain with example, how to insert data using jQuery AJAX in ASP.Net MVC Razor. 

Here data will be Post from view to Controller with help of jQuery AJAX and inserted Data using Entity Framework in ASP.Net MVC.  

Insert Data from view to controller using jQuery AJAX in ASP.Net MVC Razor



I have created tblEmp table, which contains field empId, empName, empCountry.

 CREATE TABLE [dbo].[tblEmp]
       [empId] [int] IDENTITY(1,1) NOT NULL,
       [empName] [varchar](50) NULL,
       [empCountry] [varchar](50) NULL


In Controller there are two Action methods.

Index and InsertData, i have used Index method for showing index page view. 

i have used InsertData method for inserting record using Entity Framework.  

i have posted data from view to Action method InsertData for Inserting data to Database,


        public ActionResult Index()
            return View();
        public JsonResult  InsertData(Employee obj)
            string status = string.Empty;
                status = "Record inserted successfully.";

            catch (Exception ex)
                status = "!!!there are some problem.";
            return Json(status, JsonRequestBehavior.AllowGet);



           I have created Employee model with properties empId, empName, empCountry  


    public class Employee
        public int empId { get; set; }
        public string empName { get; set; }
        public string empCountry { get; set; }



In View there are two TextBoxes and a Button. i have called jQuery AJAX function submitData on Button click.

When button click, it will call jQuery AJAX function submitData, i will take all textbox value using jQuery and post data to Controller, which contain ActionResult(InsertData),
will insert data using Entity Framework after that it will return success or failure message to ajax success method ,

if data inserted successfully then i will show alert message on success method.

<!DOCTYPE html>
    <table >
            <td style="width: 155px">
                Emp Name<br/>
                <input type="text" id="txtEmpName" style="width:145px"/>
            <td style="width: 155px">
                <input type="text" id="txtEmpCountry" style="width:145px"/>
            <td style="width: 200px">
                <input type="button" id="btnSubmit" onclick="submitData();" value="Submit"/>
    <script type="text/javascript" src=""></script>
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
        function submitData () {
            var txtEmpName = $("#txtEmpName").val();
            var txtEmpCountry = $("#txtEmpCountry").val();
            var obj = {};
            obj.empName = txtEmpName;
            obj.empCountry = txtEmpCountry;
                type: "POST",
                url: "/Home/InsertData",
                data: JSON.stringify(obj),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                error: function (errormsg) {


Output Screen




I hope this article will help you to learn, How to insert data using jQuery AJAX in ASP.Net MVC, if you like this article please share Us on

Shubham Kumar

Shubham Kumar is a founder of AspNetBugs, .NET Team Leader. He has very good experience with designing and developing enterprise-scale applications. He is exprience in ASP.NET MVC,ASP.NET C#, ASP.NET Core, AngularJS, Web API, EPPlus, SQL, Entity Framework, JavaScript, jQuery, Kendo, Windows services etc.