使用AJAX传递JSON / JavaScript的数据/对象为C#功能 [英] Pass json/javascript data/objects to c# function using ajax
问题描述
我用FullCalendar( http://arshaw.com/fullcalendar/ </A>),我需要帮助与传递数据的背后我的ASP.net页的页code。使用JSON到C#的功能。
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.
我使用JSON数据加载像这样在我的FullCalendar Web应用程序:
I am using json to load data like so in my FullCalendar web application:
后面的 code:
[WebMethod]
public static List<Event> GetEvents()
{
List<Event> events = new List<Event>();
events.Add(new Event()
{
EventID = 1,
EventName = "EventName 1",
StartDate = DateTime.Now.ToString("MM-dd-yyyy"),
EndDate = DateTime.Now.AddDays(2).ToString("MM-dd-yyyy"),
EventColor = "red"
});
events.Add(new Event()
{
EventID = 2,
EventName = "EventName 2",
StartDate = DateTime.Now.AddDays(4).ToString("MM-dd-yyyy"),
EndDate = DateTime.Now.AddDays(5).ToString("MM-dd-yyyy"),
EventColor = "green"
});
return events;
}
asp.x页:
events: function(start, end, callback)
{
$.ajax(
{
type: 'POST',
contentType: 'application/json',
data: "{}",
dataType: 'json',
url: "Calendar.aspx/GetEvents",
cache: false,
success: function (response) {
var events = $.map(response.d, function (item, i) {
var event = new Object();
event.id = item.EventID;
event.start = new Date(item.StartDate);
event.end = new Date(item.EndDate);
event.title = item.EventName;
event.color = item.EventColor;
return event;
})
callback(events);
},
error: function (err) {
alert('Error');
}
});
},
这是工作的罚款。现在我想通过调用一个函数,并传递给它的数据来保存数据。以下是我迄今所做的:
This is working fine. Now I want to save data by calling a function and passing it data. Here is what I have done so far:
后面的 code:
[WebMethod]
public static bool SaveEvents(List<Event> events)
{
//iterate through the events and save to database
return true;
}
asp.x页
function save() {
var eventsFromCalendar = $('#calendar').fullCalendar('clientEvents');
var events = $.map(eventsFromCalendar, function (item, i) {
var event = new Object();
event.id = item.id;
event.start = item.start;
event.end = item.end;
event.title = item.title;
event.color = item.color;
return event;
});
$.ajax(
{
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(events), **<-- I have to pass data here but how???**
dataType: 'json',
url: "Calendar.aspx/SaveEvents",
cache: false,
success: function (response) {
alert('Events saved successfully');
},
error: function (err) {
alert('Error Saving Events');
}
});
return false;
}
以上阿贾克斯后就是我需要帮助。变量事件contais所有FullCalendar活动信息。 我如何通过数据的'事件'的功能SaveEvents? 我可以改变SaveEvents签名,如果需要的话。
The above ajax post is where I need help with. The variable events contais all the FullCalendar event info. How do I pass the data 'events' to the function 'SaveEvents'? I can change the SaveEvents signature if need be.
编辑 如果我改变我的C#函数使用数组一样S0:
EDIT If I change my c# function to use an array like s0:
[WebMethod]
public static bool SaveEvents(Event[] newEvents)
{
return true;
}
和通过传递以下数据:
data: JSON.stringify({ newEvents: events }),
然后函数SaveEvents被称为大小为4的数组,但所有的数据值都为空,这是为什么?
Then the function 'SaveEvents' gets called with an array of size 4 but all the data values are null, why is this?
感谢
推荐答案
你有一个C#类
public class Events
{
public int EventID {get; set;}
public string EventName {get;set;}
public StartDate {get; set;}
public string end {get; set;}
public string color {get; set;}
}
因此,采取JSON数组我把它静态的。你可以得到你的,但必须是相同的格式
So take json array I have take it static. You can get yours but must be in same format
newEvents=[]; // Array which would be provided to c# method as data
an_event = { EventID = 1,
EventName = "EventName 1",
StartDate = "2015-01-01",
EndDate = "2015-01-03",
EventColor = "red"};
newEvents.push(an_event);
an_event = {EventID = 2,
EventName = "EventName 2",
StartDate = "2015-01-01",
EndDate = "2015-01-07",
EventColor = "green"};
newEvents.push(an_event);
现在Ajax请求其通过JSON对象张贴网址
Now the ajax request which passes JSON objects to posted URL
$.ajax
({
type: 'POST',
contentType: 'json',
data: newEvents, // Pass data as it is. Do not stringyfy
dataType: 'json',
url: "Calendar.aspx/SaveEvents",
cache: false,
success: function (response) {
alert('Events saved successfully');
},
error: function (err) {
alert('Error Saving Events');
}
});
更新:
要确保有没有别的错误,请测试您保存事件。如果这样下去,上面的罚款应该为好,因为它是为我工作:)
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]
public static bool SaveEvents(string EventName)
{
//iterate through the events and save to database
return true;
}
$.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');
}
});
这篇关于使用AJAX传递JSON / JavaScript的数据/对象为C#功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!