public ActionResult Add(Control ctl) { if (ctl.ModuleID == null) { return(Content("请选择所属模块")); } if (ctl.Name == "") { return(Content("请输入控制器名称")); } if (ctl.Url == "") { return(Content("请输入URL")); } #region 生成控制器编码 //查找模块编码 var module = ModuleService.LoadEntities(u => u.ID == ctl.ModuleID).FirstOrDefault(); //查找同一模块的控制器 var ctlList = ControlService.LoadEntities(u => u.ModuleID == ctl.ModuleID); string ctlCode; var ctlCodeList = (from u in ctlList orderby u.Code descending select new { u.Code }); if (ctlCodeList.Any()) { ctlCode = ctlCodeList.Take(1).ToList()[0].Code; string strLs = ctlCode.Substring(ctlCode.Length - 3, 3); int intLs = int.Parse(strLs); intLs++; string strNewLs = ctlCode.Substring(0, ctlCode.Length - 3) + intLs.ToString("D3"); ctl.Code = strNewLs; } else { ctl.Code = module.Code + "001"; } #endregion ctl = initEntity(ctl); //ctl.Init(); //ctl.ID = Guid.NewGuid(); ControlService.Add(ctl); if (ControlService.SaveChanges() > 0) { return(Content("ok")); } return(Content("添加失败了")); }