public ActionResult Edit(EF.Account account)
        {
            //客户端已处理ajax重复提交问题(ajax提交一次,表单再提交一次)
            string msg = "保存失败";

            EF.Account editAccount = account, findAccount = null;
            try
            {
                if (account != null)
                {
                    // TODO: Add insert logic here
                    //添加帐户关联的角色

                    using (var ct = new DB())
                    {
                        //account = ct.Account.Where(a => a.AccountID == account.AccountID).Include("Role").FirstOrDefault();
                        findAccount = ct.Account.Where(a => a.AccountID == editAccount.AccountID).Include("Role").FirstOrDefault();
                        //List<Role> rolemodeles = account.Role.ToList();
                        List <Role> rolemodeles = findAccount.Role.ToList();
                        //删除所有数据,再重新添加
                        foreach (Role role in rolemodeles)
                        {
                            account.Role.Remove(role);
                        }

                        //找到添加的对应的角色
                        if (!string.IsNullOrWhiteSpace(Request.Form["selectedRoles"]))
                        {
                            string       roles      = Request.Form["selectedRoles"];
                            DbSet <Role> set        = ct.Set <Role>();
                            string       sql        = "select * from Role where RoleID in (" + roles + ")";
                            List <Role>  rolemodels = set.SqlQuery(sql).ToList();
                            account.UpdateTime = DateTime.Now;
                            //accountroles.Role = rolemodels;
                            //给账户添加角色
                            foreach (Role role in rolemodels)
                            {
                                findAccount.Role.Add(role);
                            }
                        }
                        ct.SaveChanges();
                    }
                    //编辑帐户
                    int result = accountDAL.Update(editAccount);
                    if (result < 0)
                    {
                        return(Content(msg));
                    }
                    msg = "保存成功";
                    return(Content(msg));
                }
                return(Content(msg));
            }
            catch
            {
                //return View();
                return(Content(msg));
            }
        }
        public ActionResult Create(EF.Account account)
        {
            string msg = "添加失败";

            try
            {
                if (account != null)
                {
                    if (Convert.ToInt32(accountDAL.GetRecordCount(string.Format("AccountName='{0}' ", account.AccountName))) < 1)
                    {
                        // TODO: Add insert logic here
                        //添加帐户
                        using (var ct = new DB())
                        {
                            // ct.Account.Add(account);
                            account.AccountID = accountDAL.Insert(account);
                            if (account.AccountID <= 0)
                            {
                                return(Content(msg));
                            }
                            //添加帐户关联的角色
                            if (!string.IsNullOrWhiteSpace(Request.Form["selectedRoles"]))
                            {
                                string roles = Request.Form["selectedRoles"];
                                int    maxid = ct.Account.Max(item => item.AccountID);
                                //找到新添加账户
                                Account accountmodel = ct.Account.FirstOrDefault(x => x.AccountID == maxid);

                                //找到添加的对应的角色
                                DbSet <Role> set = ct.Set <Role>();
                                string       sql = "select * from Role where RoleID in (" + roles + ")";
                                //var rolemodels =  from s in ct.Role
                                //                  from p in roles
                                //                  where s.RoleID==p
                                //                         select s;
                                List <Role> rolemodels = set.SqlQuery(sql).ToList();
                                //给账户添加角色
                                foreach (Role role in rolemodels)
                                {
                                    accountmodel.Role.Add(role);
                                }
                            }
                            ct.SaveChanges();
                            msg = "添加成功";
                            return(Content(msg));
                        }
                    }
                    else
                    {
                        msg = "帐户名[" + account.AccountName + "]已存在";
                        //return RedirectToAction("Index");
                        return(Content(msg));
                    }
                }
                return(Content(msg));
            }
            catch
            {
                //return View();
                return(Content(msg));
            }
        }