我需要根据“上级部门”的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); }
但是我觉得这样写太罗嗦了,哪位大侠有没有更好的方法,谢谢。
Action中 确实不应该有太多的业务逻辑 Action主要是 接收页面请求 执行什么操作 可以讲action中的代码封装到一个类里 不过你说如何让代码精简 我到没想出来