I wanted to share what I learned today with all of you. My question is:
Can you pass a JSON object from JavaScript code to a .NET method? For instance:
var task = { Title: $("#titlenew input", $(newTaskRow)).val(), StartDate: $("#startnew input", $(newTaskRow)).val(), EndDate: $("#endnew input", $(newTaskRow)).val(), EstimatedHours: $("#esthrsnew input", $(newTaskRow)).val(), PredecessorsOutlineNumbers: $("#depnew input", $(newTaskRow)).val(), OutlineNumber: $("#ordernew", $(newTaskRow)).text() }; PageMethods.AddTask(task, saveNewTaskCompleted, saveNewTaskFailed);
And if you can, what type of .NET object should my web method accept?
I found out that yes, you can pass the JSON object to the page method, and it appears as a dictionary (Of String, String). Therefore, my signature on the Internet is as follows:
<System.Web.Services.WebMethod()> _ Public Shared Sub AddTask(ByVal taskJson As Dictionary(Of String, String)) Dim oTask As New Task() oTask.Title = taskJson("Title") ' all other accesses to the JSON object here End Sub
source share