首页 新闻 会员 周边

mvc2中关于id与值的转换

0
悬赏园豆:30 [已解决问题] 解决于 2012-05-23 16:19

我需要根据“上级部门”的id得到他的name,在helper中代码如下:

 public Dept Read(object ID)
        {
            using (UUMContext uc = new UUMContext())
            {
                Dept dept = uc.Depts.Find(Convert.ToInt32(ID));
                return dept;
            }
        }

controllor中代码

 public class ExtendDept : Dept
    {
        private string parentDeptName;

        public string ParentDeptName
        {
            get { return parentDeptName; }
            set { parentDeptName = value; }
        }
    }

 

 public ActionResult List(int? pageIndex, string deptName, string deptCode, string orgID,string orgParent, string deptParent, string deptLeader, string deptPhone, string deptState)
        {
            ViewData["DeptParent"] = new SelectList(deptHelper.GetAllListForSel(), "DeptID", "DeptName");
            ViewData["OrgID"] = new SelectList(orgHelper.GetAllListForSel(), "OrgID", "OrgName");
            ViewData["deptState"] = stateItems;
            IList<Dept> lists = deptHelper.GetLists(1, 15, deptName, deptCode, orgID, orgParent, deptParent, deptLeader, deptPhone, deptState);
            IList<ExtendDept> items = new List<ExtendDept>();
            if (lists != null)
            {
                foreach (Dept d in lists)
                {
                    ExtendDept pd = new ExtendDept();
                    //pd.ParentDeptName = deptHelper.Read(d.DeptParent).DeptName;
                    Dept dept = deptHelper.Read(d.DeptParent);
                    if (dept != null)
                    {
                        pd.ParentDeptName = dept.DeptName;
                    }
                    pd.Org = d.Org;
                    pd.DeptName = d.DeptName;
                    pd.DeptCode = d.DeptCode;
                    pd.OrgID = d.OrgID;
                    pd.DeptID = d.DeptID;
                    pd.DeptLeader = d.DeptLeader;
                    pd.DeptPhone = d.DeptPhone;
                    pd.DeptState = d.DeptState;
                    items.Add(pd);
                }
            }
            return View(items);
        }

 

但是我觉得这样写太罗嗦了,哪位大侠有没有更好的方法,谢谢。

 
孙艺玮的主页 孙艺玮 | 初学一级 | 园豆:141
提问于:2012-05-21 17:23
< >
分享
最佳答案
0

Action中 确实不应该有太多的业务逻辑 Action主要是 接收页面请求 执行什么操作 可以讲action中的代码封装到一个类里 不过你说如何让代码精简 我到没想出来

收获园豆:30
天地盟主 | 菜鸟二级 |园豆:251 | 2012-05-21 18:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册