Ext.regModel('Depertment', {
fields: [
{ name: 'DEPTNO', type: 'string' },
{ name: 'DEPTNAME', type: 'string' }
]
}); var departmentStore = new Ext.data.JsonStore({
model: 'Depertment',
proxy: {
type: 'ajax',
url: '/HumanResource/Departments',
fields: [
'DEPTNO',
'DEPTNAME'
],
reader: {
type: 'json',
root: 'data'
},
actionMethods: {
read: 'POST'
}
}
});[HttpPost]
public ActionResult Departments()
{
using (HREntities hr = new HREntities())
{
List<DEPARTMENT> departments = hr.DEPARTMENT.ToList();
return Json(new { data = departments }, JsonRequestBehavior.AllowGet);
}
}
在控制器的action中,可以看到departments的值,为什么到客户端departmentStore却没有值?
这里大概分享下,用实体数据类型,上面的action用了using,这样不行,因为会自动关闭连接,导致load时候连接不上,出现了 ‘此 ObjectContext 实例已释放,不可再用于需要连接的操作。’这种错误,就是DB连接被关闭了。
我把using去掉,每次都实例化一个实体对象,解决了这个问题。
接下来又遇到了一个错误 ‘序列化类型为“Mvc3Ext4.Models.DEPARTMENT”的对象时检测到循环引用。’
查了下,是因为实体数据类型中这个对象存在一个1:N的连接,最简单的方法就是直接把这个关联属性取消掉。
参考文章 ADO.NET 实体数据模型 异常-“序列化类型为 XX 的对象时检测到循环引用” 网址:http://archive.cnblogs.com/a/1956117/修改后的代码
[HttpPost]
public JsonResult Departments()
{
HREntities hr = new HREntities();
hr.ContextOptions.LazyLoadingEnabled = false;
List<DEPARTMENT> departments = hr.DEPARTMENT.OrderBy(p => p.DEPTNO).ToList();
//var departments = from dep in hr.DEPARTMENT select dep;
//return Json(departments);
return Json(new { data = departments }); }