首页新闻找找看学习计划

json在序列化list时Detached错误怎么解决

0
悬赏园豆:20 [待解决问题]
1 public string GetAllCity(int provinceid)
2         {
3             DAL.ProCityTown procitytown = new ProCityTown();
4             IList<City> list = procitytown.GetAllCity(provinceid);
5             string strjson = JsonConvert.SerializeObject(list, Formatting.Indented);//将对象u转化为json格式 
6             return strjson;
7         }
 1 public IList<City> GetAllCity(int provinceid)
 2         {
 3             IList<City> list = null;
 4             using (ISession session = this.factoy.OpenSession())
 5             {
 6                 ITransaction tran = session.BeginTransaction();
 7                 list = session.QueryOver<City>()
 8                     .Where(c => c.Province.ProvinceID == provinceid)
 9                        .List();
10 
11 
12 
13                 tran.Commit();
14                 session.Close();
15 
16                 return list;
17 
18             }
19         }

 

问题补充:

DAL: 

public IList<Province> GetAllProvince()
        {
            IList<Province> list = null;
            using (ISession session = this.factoy.OpenSession())
            {
                ITransaction tran = session.BeginTransaction();
                list = session.QueryOver<Province>()
                       .List();
                tran.Commit();
                session.Close();
                return list;
            }
        }
        [Test]
        public IList<City> GetAllCity(int provinceid)
        {
            IList<City> list = null;
            using (ISession session = this.factoy.OpenSession())
            {
                ITransaction tran = session.BeginTransaction();
                list = session.QueryOver<City>()
                     .Inner.JoinQueryOver<Province>(c => c.Province)
             .Where(p => p.ProvinceID == provinceid)
             .List();
                tran.Commit();
                session.Close();
            }
            return list;
        }

        public IList<DistrictDTO> GetAllTown(int cityid)
        {
            DistrictDTO dto = null;
            IList<DistrictDTO> retList = null;
            using (ISession session = this.factoy.OpenSession())
            {
                ITransaction tran = session.BeginTransaction();
                //   list = session.QueryOver<District>()
                //        .Inner.JoinQueryOver<City>(d => d.City)
                //.Where(c => c.CityID == cityid)
                //.List();
                //   tran.Commit();
                //   session.Close();
                retList = session.QueryOver<District>()
                    .Inner.JoinQueryOver<City>(d => d.City)
                    .Where(c => c.CityID == cityid)
          .SelectList(list => list
                .Select(d => d.DistrictID).WithAlias(() => dto.DistrictID)
               .Select(d => d.DistrictName).WithAlias(() => dto.DistrictName)
               .Select(d => d.City).WithAlias(() => dto.City)
           )
           .TransformUsing(Transformers.AliasToBean<DistrictDTO>())
            .List<DistrictDTO>();
                tran.Commit();
                session.Close();
            }
            return retList;
        }

BLL:

public string GetAllProvince()
        {
            DAL.ProCityTown procitytown = new ProCityTown();
            IList<Province> list = procitytown.GetAllProvince();
            string strjson = JsonConvert.SerializeObject(list, Formatting.Indented);//将对象u转化为json格式
            return strjson;
        }

        public string GetAllCity(int provinceid)
        {
            DAL.ProCityTown procitytown = new ProCityTown();
            IList<City> list = procitytown.GetAllCity(provinceid);
            string strjson = JsonConvert.SerializeObject(list, Formatting.Indented);//将对象u转化为json格式
            return strjson;
        }

        public string GetAllTown(int cityid)
        {
            DAL.ProCityTown procitytown = new ProCityTown();
            IList<DistrictDTO> list = procitytown.GetAllTown(cityid);
            string strjson = JsonConvert.SerializeObject(list, Formatting.Indented);//将对象u转化为json格式
            return strjson;
        }

用NHibernate实现的省市县三级联动,在bll的getalltown时会报错得到的list是readonly

SnowO的主页 SnowO | 初学一级 | 园豆:182
提问于:2012-12-05 19:55
< >
分享
所有回答(2)
0

 

System.Web.Script.Serialization.JavaScriptSerializer oSerializer =           new System.Web.Script.Serialization.JavaScriptSerializer(); string sJSON = oSerializer.Serialize(oList);

http://www.baidu.com/s?wd=how+to+list+type+to+json+in+C%23&rsv_bp=0&rsv_spt=3&rsv_n=2&rsv_sug3=1&rsv_sug1=1&rsv_sug4=42&inputT=609

 

 public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e1)
        {
            List<Employee> eList = new List<Employee>();
            Employee e = new Employee();
            e.Name = "Minal";
            e.Age = 24;

            eList.Add(e);

            e = new Employee();
            e.Name = "Santosh";
            e.Age = 24;

            eList.Add(e);

            string ans = JsonConvert.SerializeObject(eList, Formatting.Indented);

            string script = "var employeeList = {\"Employee\": " + ans+"};";
            script += "for(i = 0;i<employeeList.Employee.length;i++)";
            script += "{";
            script += "alert ('Name : ='+employeeList.Employee[i].Name+' 
			Age : = '+employeeList.Employee[i].Age);";
            script += "}";

            ClientScriptManager cs = Page.ClientScript;
            cs.RegisterStartupScript(Page.GetType(), "JSON", script, true);
        }
    }
    public class Employee
    {
        public string Name;
        public int Age;
    }

http://www.codeproject.com/Articles/78928/Create-JSON-from-C-using-JSON-Library
再见2019 | 园豆:11616 (专家六级) | 2012-12-05 20:04

 没用的,因为list是readonly只能读,不能改

支持(0) 反对(0) SnowO | 园豆:182 (初学一级) | 2012-12-05 20:10
0

检查下对象是否可以被序列化

az235 | 园豆:8263 (大侠五级) | 2012-12-06 09:39

上面的省province和市city的list都可以序列化,但是就是区district不能,困惑了我好几天了,就是报所得到的distric的list是readonly,在NHibernate3.3的api有一章讲的是readonly Entity,里面好像是单一或者单向关联查询后得到的数据自动为readonly,我的表关联是这样的,city many-to-one province,District many-to-one city。求大神解决,我可以发源码。

支持(0) 反对(0) SnowO | 园豆:182 (初学一级) | 2012-12-06 11:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册