简单的事情:我想从aspx-Page。
中调用aspx.cs中的C#函数我的C#功能(Kalender.aspx.cs):
//DATABASE READOUT
var json
try { //DIRECT CALL
var data = <%= GetEvents()%>; //<<-- throw ReferenceError: System is not defined
alert('DIRECT CALL: ' + typeof (data) + ' | ' + data[0]);
json = JSON.parse(data);
} catch (error) {
alert(error + "\r" + error.name + "\r" + error.message);
}
我的JS电话(Kalender.aspx):
public partial class Events
{
public int EventID { get; set; }
public string Subject { get; set; }
public string Description { get; set; }
public System.DateTime Start { get; set; }
public Nullable<System.DateTime> End { get; set; }
public string Color { get; set; }
public bool IsFullDay { get; set; }
}
GetEvents()中的JsonResult包含一个存储在SQL中的[Events] - Object。那么一个 我添加了一个ADO.NET实体数据模型:
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.RemoteDependency","time":"2018-04-02T11:49:50.7556362Z","tags":{"ai.internal.nodeName":"LAPTOP-81CNBL6C","ai.operation.parentId":"Y67fpD/L2MI=","ai.operation.name":"GET /Pages/Kalender","ai.operation.id":"Y67fpD/L2MI=","ai.location.ip":"::1","ai.cloud.roleInstance":"LAPTOP-81CNBL6C","ai.internal.sdkVersion":"rddf:2.2.0-738"},"data":{"baseType":"RemoteDependencyData","baseData":{"ver":2,"name":"(LocalDB)\\MSSQLLocalDB | C:\\USERS\\MARTIN\\DOCUMENTS\\VISUAL STUDIO 2017\\PROJECTS\\HOMEOFFICE\\HOMEOFFICE\\APP_DATA\\DATABASE.MDF","id":"zYD3G9N1sOI=","duration":"00:00:00.0110000","resultCode":"0","success":true,"type":"SQL","target":"(LocalDB)\\MSSQLLocalDB | C:\\USERS\\MARTIN\\DOCUMENTS\\VISUAL STUDIO 2017\\PROJECTS\\HOMEOFFICE\\HOMEOFFICE\\APP_DATA\\DATABASE.MDF","properties":{"DeveloperMode":"true"}}}}
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Request","time":"2018-04-02T11:49:48.6739718Z","tags":{"ai.operation.name":"GET /Pages/Kalender","ai.operation.id":"Y67fpD/L2MI=","ai.location.ip":"::1","ai.cloud.roleInstance":"LAPTOP-81CNBL6C","ai.internal.sdkVersion":"web:2.2.0-738"},"data":{"baseType":"RequestData","baseData":{"ver":2,"id":"Y67fpD/L2MI=","name":"GET /Pages/Kalender","duration":"00:00:02.3570000","success":true,"responseCode":"200","url":"http://localhost:56879/Pages/Kalender","properties":{"DeveloperMode":"true"}}}}
如果模型不起作用&lt;%= GetEvents()%&gt;抛出正确的错误:“模型未定义”。
错误“系统未定义”的重点是什么。我花了几个小时没找到解决办法。请帮助,感谢任何回复。
(我生命中最糟糕的事情:昨天一切正常,该死的接受JsonResult,我可以读出数据。早上我清理代码。重命名一些东西。世界末日。没有什么可以了,我从头开始重新开始,不知道出了什么问题。我很伤心。:()
编辑###
控制台输出:
date customer package
1/1/2015 12 2
2/1/2015 12 5
3/1/2015 12 2
4/1/2015 12 3
5/1/2015 12 6
6/1/2015 12 7
7/1/2015 12 5
8/1/2015 12 3
9/1/2015 12 2
答案 0 :(得分:0)
几个小时后,我找到了一个简单的解决方案。
我添加到我的C#代码:
return new JavaScriptSerializer().Serialize(json.Data);
接受我的函数返回字符串而不是JsonResult:
protected string GetEvents()
{
//DATABASE READOUT
using (DatabaseEntities dc = new DatabaseEntities())
{
var events = dc.Events.ToList();
JsonResult json = new JsonResult { Data = events, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
return new JavaScriptSerializer().Serialize(json.Data);
}
}
JavaScript很好。 [var]将理解形成并自动投射到json。