Pass user defined object to ASP.NET Webmethod from jQuery, using JSON



In .html page
  <script>
        function StoreDataInDatabse() {
            debugger;
            var Employee_Detail = {}
            Employee_Detail.Emp_Name = "Test";
            Employee_Detail.Emp_Age = "24";
            Employee_Detail.Emp_Salary = "1000";
            Employee_Detail.Emp_City = "Ban";

            $.ajax({
                type: "POST",
                url: "default.aspx/Insert_Data",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ 'Employee_Detail': Employee_Detail }), // Check this call.
                success: function () {

                    alert("Data Has Inserted")
                }
            })
        }
    </script>

<div>
 <input type="button" id="btnSave" value="Save Data" onclick="StoreDataInDatabse()"/>
               
</div>

Default.aspx.cs file

[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void Insert_Data(Employee_Detail Employee_Detail)
        {

        }

Class file
public class Employee_Detail
    {
        public string Emp_Name { get; set; }
        public string Emp_Age { get; set; }
        public string Emp_Salary { get; set; }
        public string Emp_City { get; set; }
    }

use same properties names in both places(client side and server side)

Comments

  1. AngularJS is a toolset for building the framework most suited to your application development. It is fully extensible and works well with other libraries. Every feature can be modified or replaced to suit your unique development workflow and feature needs. Read on to find out how.

    AngularJS Training in Chennai
    AngularJS Training Institute in Chennai

    AngularJS Certification Training in Chennai

    ReplyDelete

Post a Comment