I've scenario where I want to insert data into database without post back. there are around 12 to 13 fields which i need to insert. I'm passing DTO from the client side which is actually Json object. Now the problem which i'm facing is how to convert that Json object which i got in webservice to the "class" (in my case class name is User) object.
<script type="text/javascript" language="javascript">
$('#<%= btnSaveInfo.ClientID %>').click(function()
{
// Initialize the object, before adding data to it.
// { } is declarative shorthand for new Object().
var User = { };
User.FirstName = $('#<%= txtFirstName.ClientID%>').val();
User.MiddleName = $('#<%= txtMiddleName.ClientID%>').val();
User.LastName = $('#<%=txtLastName.ClientID %>').val();
User.Gender = $('table#<%= rdbGenderType.ClientID %> input:radio:checked').val();
User.DateCreated = Date.UTC();
User.Description = $('#<%= txtDescription.ClientID%>').val();
User.DOB = $('#<%= txtDOB.ClientID %>').val();
User.Location=$('#<%= txtLocation.ClientID %>').val();
var DTO = {'User' : User};
var path = 'MyWebService.asmx/AddNewUser';
$.ajax
({
type: "POST",
url: path,
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(DTO),
async:false,
success: function(msg)
{
}
});
});
as you can see i'm passing the data as data: JSON.stringify(DTO),. Now how do I convert this JSon object into
[WebMethod]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
public bool AddNewUser(Object User)
{
return UserRepository.AddNewUser(User);
}
In the above case AddNewUser method takes the object of User class. But i'm getting casting error. So how do I convert Json object to the "User" class object?