几乎每个‘数人头’项目中都会用到的功能,这里先记下来,以后直接到这里复制代码就行了,ASP.NET MVC中的使用
数据库用户表中的除了有个parentid父级ID外,我还多加了以个字段,parentpath,表示父级路径,把该用户的所有上级ID全都存起来,以,间隔,如用户ID=5的低级路径 为:,1,2,3,4, 最前面加多一个,是为了查询该用户的下级用户总数方便
下面是HTML代码:
@{
ViewBag.Title = "推荐图谱";
}
- @ViewBag.Title
| 用户名 | 注册时间 | 下级总数 |
|---|
下面是相应的后台代码:
//推荐图谱-取数据
public ActionResult TJTuPu_Ajax(int? id)
{
ArrayList arr = new ArrayList();
if (id == null)
{
//取自己
Model.Userinfo u = base.GetLoginUser();
int xjcount = udal.CalcCount($"parentpath like '%,{u.id},%'");
string state = xjcount == 0 ? "open" : "closed";
arr.Add(new { id = u.id, username = u.username, regdate = u.createtime.ToString("yyyy-MM-dd"), xjcount = xjcount, state = state, });
}
else
{
//根据ID取下一级
List
foreach (var u in list_u)
{
int xjcount = udal.CalcCount($"parentpath like '%,{u.id},%'");
string state = xjcount == 0 ? "open" : "closed";
arr.Add(new { id = u.id, username = u.username, regdate = u.createtime.ToString("yyyy-MM-dd"), xjcount = xjcount, state = state, });
}
}
return Json(arr);
}
其实以上的代码都是直接从easyui官网上的demo里复制出来的,我只是把他的后台php示例代码改为了c#而已



