以下是代码:
1)MVC控制器:
public DiagramData GetDiagramData()
{
return new DataBindingHelper().GetData();
}
2)数据助手:
public class DataBindingHelper
{
public DiagramData GetData()
{
DiagramData diagramData = new DiagramData();
return diagramData;
}
}
public class DiagramData
{
public string From
{
get { return "Moscow"; }
}
public string To
{
get { return "Saint Petersburg"; }
}
public string Color
{
get { return "Red"; }
}
public int Thick
{
get { return 2; }
}
}
3)Java脚本:
function initDataBinding() {
jQuery.ajax({
type: "GET",
//url: '@Url.Action("GetDiagramData", "Home")'
url: '/Home/GetDiagramData'
}).done(function (data) {
//data = JSON.stringify(data);
//alert(data.From);
});
答案 0 :(得分:0)
如果它是正常的控制器,它应该在服务器端:
public JsonResult GetDiagramData()
{
return Json(new DataBindingHelper().GetData(), JsonRequestBehavior.AllowGet);
}
在客户端:
function initDataBinding() {
$.ajax({
type: "GET",
url: '/Home/GetDiagramData'
}).done(function (data) {
alert(data.From); //<- this line should work
});