示例#1
0
 public async Task<ActionResult> Create(RoleViewModel roleViewModel)
 {
     if (ModelState.IsValid)
     {
         var role = new IdentityRole(roleViewModel.Name);
         var roleresult = await RoleManager.CreateAsync(role);
         if (!roleresult.Succeeded)
         {
             ModelState.AddModelError("", roleresult.Errors.First().ToString());
             return View();
         }
         return RedirectToAction("Index");
     }
     else
     {
         return View();
     }
 }
示例#2
0
        public ActionResult AddRole(RoleViewModel model)
        {
            if (model == null || !this.ModelState.IsValid)
            {
                this.TempData.Add(ModelConstants.Error, ModelConstants.ModelError);
                return this.View(model);
            }

            if (this.data.Roles.Any(x => x.Name.ToLower().Equals(model.Name.ToLower())))
            {
                this.ModelState.AddModelError("Name", "Ролята " + model.Name + " вече съществува");
                return this.View(model);
            }

            var role = this.Mapper.Map<IdentityRole>(model);
            this.data.Roles.Add(role);
            this.data.SaveChanges();
            this.TempData.Add(ModelConstants.Success, "Ролята " + model.Name + " е добавена успешно.");

            return this.RedirectToAction("Index");
        }
 //
 // GET: /Roles/Edit/Admin
 public async Task<ActionResult> Edit(string id)
 {
     if (id == null)
     {
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
     }
     var role = await RoleManager.FindByIdAsync(id);
     if (role == null)
     {
         return HttpNotFound();
     }
     RoleViewModel roleModel = new RoleViewModel { Id = role.Id, Name = role.Name };
     return View(roleModel);
 }