I am a beginner and I'm having trouble getting a list of objects called "Event" (OrderId, Date) from the MVC controller to JavaScript in the view. When I try to display the list as strings, it shows me "undefined". On the controller side, everything seems to be correct! I have been searching for my error but I can't find it. I have tried using # and without, also tried using foreach loop but I think something else might be wrong. Thank you for helping me!
The Controller :
[System.Web.Mvc.HttpGet]
public JsonResult getListEvent() {
IList<Event> ListEvent = new List<Event>();
ListEvent = DeserializeElement();
return Json(new { ListEvent = ListEvent }, JsonRequestBehavior.AllowGet);
}
private List<Event> DeserializeElement () {
List<Event> lt = new List<Event>();
document.Load("eventXml.xml");
foreach (XmlNode item in document.DocumentElement.ChildNodes) {
Event e = new Event();
e.OrderId = int.Parse(item.Attributes[0].Value);
e.Date = Convert.ToDateTime(item.Attributes[1].Value);
lt.Add(e);
}
return lt;
}
and the View :
function GetListEvent() {
debugger;
$.ajax({
type: "GET",
url: "Home/getListEvent",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (data) {
debugger;
alert( data[0].OrderId);
alert('#'+ data[1].Date);
for (var item in data ) {
alert('#' + data[item]);
}
},
error: function (response) {
debugger;
alert('error');
}
});
}