Just little modification in JSON data and AJAX request. Try with below code:
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:
[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.