I'm trying to serialize a dozen or so inputs and post this to a ASP.Net webmethod.
My question is rather than manually deserialize the JSON string and populate the object class can this be done automatically? I'll be receiving a single object not a list of objects.
I've been trying for days to accomplish this with no success, scouring lots of posts and just not finding any obvious examples to this. Sorry if I've overloaded with info just trying to cover everything.
Any help, ideas or direction is appreciated.
JQuery
function pushOwnerData(formData) { //formData is array of inputs
// [used for debug purposes]
//var test = "{owner: " + JSON.stringify(formData) + "}";
// JSON.stringify({ formData: formData })
//{ myOwner: JSON.stringify(formData) },
// [end debug tinkering]
var request = $.ajax({
url: 'records_v1.2.aspx/UpdateOwnerInfo',
method: "POST",
data: "{myOwner: " + JSON.stringify(formData) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json"
});
request.done(function (r) {
console.log('success');
});
request.fail(function (jqXHR, textStatus) {
console.log("pushOwnerData Request failed: " + textStatus);
});
}
function editOwnerDialog() {
var ownerDiv = getOwnerContent();
$(ownerDiv).dialog({
modal: true,
draggable: false,
resizable: false,
show: 'fold',
hide: 'blind',
width: 500,
height: 350,
dialogClass: 'ui-dialog-osx',
title: "Edit Owner",
buttons: {
"Save Changes": function () {
if (validateOwnerData()) {
// serialize to an array to be passed to a webmethod
var fields = $(".divTableBody").find("select,textarea,input").serializeArray();
var id = $('.owner_detail').find('.id').html();
fields.push({ name: "id", value: id });
// [used for debug purposes]
// var jsonFields = JSON.stringify(fields);
// var test = $(".divTableBody").find("select,textarea,input").serialize();
// var jsonTest = JSON.stringify(test);
// [end debug tinkering]
var result = pushOwnerData(fields);
} else {
return false;
}
},
"Cancel": function () {
$(this).dialog("destroy");
}
},
open: function () {
// create a mask for phone class
$('#owner_phone').mask("(999) 999-9999");
},
close: function () {
$(this).dialog("destroy");
}
});
};
Ex of JSON data
[{"name":"prefix","value":""},{"name":"address1","value":"1234 Stone Mountain Rd"},{"name":"first","value":"Amy"},{"name":"address2","value":"Suite 18"},{"name":"middle","value":"Marie"},{"name":"city","value":"atlanta"},{"name":"last","value":"Smith"},{"name":"state","value":"GA"},{"name":"suffix","value":""},{"name":"postalcode","value":"65472"},{"name":"phone","value":"(850) 775-3131"},{"name":"email","value":"baba@hotmail.com"},{"name":"id","value":"2501"}]
Snip of Owner class (properties have same names as json keys)
Private _id As Integer
<DataMember()>
Public Property id() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property
Private _last As String
<DataMember()>
Public Property last() As String
Get
If Not _last = "" Then
_last = Microsoft.VisualBasic.StrConv(_last, VbStrConv.ProperCase)
End If
Return _last
End Get
Set(ByVal value As String)
_last = value
End Set
End Property
Private _first As String
<DataMember()>
Public Property first As String
Get
If Not _first = "" Then
_first = Microsoft.VisualBasic.StrConv(_first, VbStrConv.ProperCase)
End If
Return _first
End Get
Set(ByVal value As String)
_first = value
End Set
End Property
Webmethod trying to populate my owner class without having to include all properties as method variables (ByVal first as string, ...etc), is this possible?
I've tried (ByVal myOwner as List(of Owner)) also which gets the method called but I end up with a list of empty objects the length of the JSON variables
<WebMethod()> _
Public Shared Function UpdateOwnerInfo(ByVal myOwner As Owner) As String
Dim result As Boolean = False
'ToDo
Return result
End Function