[英]How to return in C# and access from Ajax a navigation property of a returned object model?
I have two tables in the database: 我在数据库中有两个表:
public partial class Department
{
public Department()
{
this.Employees = new HashSet<Employee>();
}
public int Id { get; set; }
public string DepName { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
And: 和:
public partial class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public System.DateTime BirthDate { get; set; }
public decimal Salary { get; set; }
public int Department { get; set; }
public virtual Department Department1 { get; set; }
}
In C# I get the Employee like this 在C#中,我得到这样的Employee
public JsonResult GetSome()
{
CompanyEntities dbc = new CompanyEntities();
dbc.Configuration.ProxyCreationEnabled = false;
return Json(dbc.Employees.ToList(), JsonRequestBehavior.AllowGet);
}
I receive it in ajax: 我用ajax收到了它:
$("#btnSome").on("click", function () {
$("#tb").empty();
$.ajax({
type: "Get",
url: "/AjaxData/GetSome",
success: function (data) {
$.each(data, function (i, el) {
$("#some").append(el.Name);
});
},
fail: function () { $("#error").append("No data"); }
});
});
It prints the names as expected, and also I can access all the other properties that represent the columns of the Employee
table (in the database). 它按预期方式打印名称,并且我还可以访问代表
Employee
表(在数据库中)各列的所有其他属性。
I need to access the department name that the Employee
instance belongs to. 我需要访问
Employee
实例所属的部门名称。 For that I need to access the navigation property Department1
of the Employee
instance. 为此,我需要访问
Employee
实例的导航属性Department1
。 So I changed iteration to this: ( but it doesn't print anything ) 所以我将迭代更改为此:( 但它不显示任何内容 )
$.each(data, function (i, el) {
$("#some").append(el.Department1.DepName);
});
Then I included Department1
to the return data like this: ( but it still doesn't print anything ) 然后,我将
Department1
包含在返回数据中,如下所示:( 但它仍然不显示任何内容 )
CompanyEntities dbc = new CompanyEntities();
dbc.Configuration.ProxyCreationEnabled = false;
return Json(dbc.Employees.Include("Department1").ToList(), JsonRequestBehavior.AllowGet);
So: How to return in C# and access from Ajax a navigation property of a returned object model? 因此:如何在C#中返回并从Ajax访问返回的对象模型的导航属性?
Your code works great. 您的代码效果很好。 It does not load it until you make a request to it.
直到您发出请求,它才会加载它。 You misunderstood I think lazy loading...it happens only on the server , you cannot tell EF to do lazy loading from javascript( accessing navigation from javascript does tell EF to do lazy loading).
您误解了我认为延迟加载...它仅在服务器上发生,您不能告诉EF从javascript进行延迟加载(从javascript访问导航确实告诉EF进行延迟加载)。 As far as you need the Departement1, navigation , you have to do eager loading when you access from c#
就需要Departement1导航而言,当您从c#访问时,您必须急切加载。
return json(dc.Employees.Include(e=>e.Department1), JsonRequestBehavior.AllowGet);
You have to use the directive using System.Data.Entity;
您必须
using System.Data.Entity;
使用指令using System.Data.Entity;
I have attached the javascript debug output on chrome 我已经在chrome上附加了javascript debug输出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.