前提需要替换查询得到的List当中的某个字段的值,替换规则有一个mapping关系
尝试代码 有问题 无法获取任何数据
/// <summary>
/// 获取Treegrid的List
/// </summary>
/// <returns></returns>
public JsonResult GetOrgnizationList()
{
IQueryable<Rights_Sys_Organization> list = db.Rights_Sys_Organization;
return Json(new treegrid
{
rows = list.Select(
s => new
{
_parentId = s.ParentCode,
OrganizationName = s.OrganizationName,
EnglishName = s.EnglishName,
OrganizationType = s.OrganizationType,
States = SysDictionaryCollection.GetFieldTextByDicTypeAndValue("DicType",s.States),
OrganizationCode = s.OrganizationCode,
Order = s.Order
}).OrderBy(c => c.Order)
}, JsonRequestBehavior.AllowGet);
}
正常的代码
/// <summary>
/// 获取Treegrid的List
/// </summary>
/// <returns></returns>
public JsonResult GetOrgnizationList()
{
IQueryable<Rights_Sys_Organization> list = db.Rights_Sys_Organization;
return Json(new treegrid
{
rows = list.Select(
s => new
{
_parentId = s.ParentCode,
OrganizationName = s.OrganizationName,
EnglishName = s.EnglishName,
OrganizationType = s.OrganizationType,
States = s.States,
OrganizationCode = s.OrganizationCode,
Order = s.Order
}).OrderBy(c => c.Order)
}, JsonRequestBehavior.AllowGet);
}
暂时未找到解决办法 如果有人知道怎么解决请留言 谢谢!
2013-08-05
解决办法:由于dynamic类型不能修改列表中的值(貌似是这样说的),所以解决办法是先将list给拿出来tolist(),然后foeach改变值,改完之后再去new row;代码如下:
public JsonResult GetOrgnizationList()
{
IQueryable<Rights_Sys_Organization> list = db.Rights_Sys_Organization; List<Rights_Sys_Organization> rlist = list.ToList(); foreach (var item in rlist)
{
item.States = SysDictionaryCollection.GetFieldTextByDicTypeAndValue("OrgStates", item.States);
} foreach (var item in rlist)
{
item.OrganizationType = SysDictionaryCollection.GetFieldTextByDicTypeAndValue("OrgType", item.OrganizationType);
} dynamic rows = rlist.Select(
s => new
{
_parentId = s.ParentCode,
OrganizationName = s.OrganizationName,
EnglishName = s.EnglishName,
OrganizationType = s.OrganizationType,
States = s.States,
OrganizationCode = s.OrganizationCode,
Order = s.Order
}).OrderBy(c => c.Order); return Json(new treegrid
{
rows = rows
}, JsonRequestBehavior.AllowGet);
}