Click here to Skip to main content
15,904,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to insert data through POSTRequest in clientside code(jquery ajax call).
When the request goes to controller methods i am not able to retrieve the data sent.When it hits the web api controlller data is null.

What I have tried:

Clientside code:

JavaScript
function AddUser()
        {         
            var user = { UserID: $("#UidTxt").val, UserName: $("#UnameTxt").val, Password: $("#PwdTxt").val, Email: $("#EmailTxt").val, Address: $("#AddrTxt").val, DOB: $("#DobTxt").val, Phone: $("#PhoneTxt") };
            $.ajax({
                url: 'http://localhost:61540/api/UserDetails/AddUser',
                type: POST,
                data: JSON.stringify(user),
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    alert("success! data is posted/inserted.")
                },
                error: function (xhr, status) {
                    alert(xhr.ResponseText);
                }
            });
        }

Web Api Controller Methods:

C#
[System.Web.Http.AcceptVerbs("POST")]
[ActionName("AddUser")]
[System.Web.Http.HttpPost]
[System.Web.Http.HttpOptions]
public List<UserDetails> AddUser(UserDetail oUserList)
{
    if (oUserList != null)
    {
    db.UserDetails.Add(oUserList);
    db.SaveChanges();
    }
    var userList = db.UserDetails.ToList();
    return DTOConverter.ConvertUserDetail(userList);
}


While Debugging,the call hits the controller methods and i find oUserList object is null.
Am i passing the data correctly? correct me if am wrong.
Thanks in advance .
Posted
Updated 18-Feb-16 20:31pm
Comments
Sreekanth Mothukuru 19-Feb-16 2:20am    
Does your UserDetail class has the same properties that you are passing through json object ?
Try changing the client side object name from "user" to "oUserList"

1 solution

Just little modification in JSON data and AJAX request. Try with below code:

Javascript:
JavaScript
function AddUser()
{
	var user = { oUserList : { UserID: $("#UidTxt").val, UserName: $("#UnameTxt").val, Password: $("#PwdTxt").val, Email: $("#EmailTxt").val, Address: $("#AddrTxt").val, DOB: $("#DobTxt").val, Phone: $("#PhoneTxt") }};
	$.ajax({		
		url: 'http://localhost:61540/api/UserDetails/AddUser',
		type: "POST",
		data: JSON.stringify(user),
		dataType: "json",
		contentType: "application/json",
		success: function (data) {
			alert("success! data is posted/inserted.")
		},
		error: function (xhr, status) {
			alert(xhr.ResponseText);
		}
	});
}

WebAPI:
C#
[System.Web.Http.AcceptVerbs("POST")]
[ActionName("AddUser")]
[System.Web.Http.HttpPost]
[System.Web.Http.HttpOptions]
public List<userdetails> AddUser(UserDetail oUserList)
{
    if (oUserList != null)
    {
    db.UserDetails.Add(oUserList);
    db.SaveChanges();
    }
    var userList = db.UserDetails.ToList();
    return DTOConverter.ConvertUserDetail(userList);
}


Note: Make sure that UserDetail class contains all properties like UserID, UserName, Password etc.
 
Share this answer
 
v2
Comments
Member 1097736 20-Feb-16 10:16am    
The problem i was facing is i was not able to run only in NON IE browsers.It works fine in Internet Exploree.Can u suggest me any ideas on this issue>?
[no name] 20-Feb-16 12:40pm    
Find out the error by pressing in your browser. On pressing f12 you will see developer tool. In developer tool you will get the error in red color and search the issue in google.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900