Pass Json/javascript Data/objects To C# Function Using Ajax
I'm using FullCalendar (http://arshaw.com/fullcalendar/) and I need help with passing data using json to a c# function in the code behind page of my ASP.net page. I am using json t
Solution 1:
Obvious from your code that you have a c# class as
publicclassEvents
{
publicint EventID {get; set;}
publicstring EventName {get;set;}
public StartDate {get; set;}
publicstring end {get; set;}
publicstring color {get; set;}
}
So take json array. A sample here // Array which would be provided to c# method as data
varnewEvents=[
{
EventID :1,
EventName :"EventName 1",
StartDate :"2015-01-01",
EndDate :"2015-01-03",
EventColor :"red"
},
{
EventID :2,
EventName :"EventName 2",
StartDate :"2015-01-02",
EndDate :"2015-01-04",
EventColor :"green"
}
];
Now the ajax request which passes JSON objects to posted URL
$.ajax
({
type: 'POST',
contentType: 'json',
data: {newEvents:newEvents},// Pass data as it is. Do not stringyfydataType: 'json',
url: "Calendar.aspx/SaveEvents"success: function (response) {
alert('Events saved successfully');
},
error: function (err) {
alert('Error Saving Events');
}
});
Update :
To make sure that there is nothing else wrong please test your Save Events as. If it goes fine above should work as well, As it is working for me :)
[WebMethod]
publicstaticbool SaveEvents(string EventName)
{
//iterate through the events and save to databasereturntrue;
}
$.ajax
({
type: 'POST',
contentType: 'json',
data: {EventName:'myevet 1'},
url: "Calendar.aspx/SaveEvents",
cache: false,
success: function (response) {
alert('Events saved successfully');
},
error: function (err) {
alert('Error Saving Events');
}
});
Post a Comment for "Pass Json/javascript Data/objects To C# Function Using Ajax"