2016-12-15 17 views
0

因此,我有一個後臺,其中包含一個列出所有用戶的頁面和一個列出所有角色的頁面。當我編輯用戶時,我有一個包含圖片中所有角色的下拉菜單。但是我想確保每當我點擊「更新」時,用戶都通過AspNetUserRole表連接到該特定角色。我不知道如何做到這一點:/身份證都是Guid Id's。從後臺下拉菜單中分配用戶到角色

這裏是我的UserController.cs:

namespace Overnight.WWW.Areas.Backoffice.Controllers 
    { 
     [Area("Backoffice")] 
     public class UserController : BaseController 
     { 
      private readonly UserManager<ApplicationUser> _userManager; 
      private readonly RoleManager<ApplicationRole> _roleManager; 
      public UserController(ApplicationDbContext applicationDbContext, UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager):base(applicationDbContext) 
      { 
       _userManager = userManager; 
       _roleManager = roleManager; 

      } 

      public async Task<IActionResult> Index(string sortParam, string searchString) 
      { 

       var model = await ApplicationDbContext.Users.OrderBy(o => o.Email).ToListAsync(); 

       if (this.Request.Headers["X-Requested-With"] == "XMLHttpRequest") 
       { 
        return PartialView("_ListPartial", model); 
       } 

       return View(model); 
      } 


      [HttpGet] 
      public async Task<IActionResult> Edit(Guid id) 
      { 
       if (id == null) 
       { 
        return new StatusCodeResult(400); 
       } 

       var model = await ApplicationDbContext.Users.FirstOrDefaultAsync(m => m.Id == id); 

       if(model == null) 
       { 
        return RedirectToAction("Index"); 
       } 

       var viewModel = await ViewModel(model); 

       return View(viewModel); 
      } 

      [HttpPost] 
      [ValidateAntiForgeryToken] 
      public async Task<IActionResult> Edit(UserViewModel model, Guid id) 
      { 
       var alert = new Alert(); 

       try 
       { 
        if(!ModelState.IsValid) 
        { 
         alert.Message = alert.ExceptionMessage = ApplicationDbContextMessage.INVALID; 
         throw new Exception(); 
        }  


        var originalModel = ApplicationDbContext.Users.FirstOrDefault(m => m.Id == id); 


        if(originalModel == null) 
        { 
         alert.Message = alert.ExceptionMessage = ApplicationDbContextMessage.NOTEXISTS; 
         throw new Exception(); 
        } 


        originalModel.Email = model.ApplicationUser.Email; 

        ApplicationDbContext.Users.Attach(originalModel); 


        ApplicationDbContext.Entry(originalModel).State = EntityState.Modified; 

        if (await ApplicationDbContext.SaveChangesAsync() == 0) 
        { 
         alert.Message = alert.ExceptionMessage = ApplicationDbContextMessage.EDITNOK; 
         throw new Exception(); 
        } 

        alert.Message = ApplicationDbContextMessage.EDITOK; 
        return RedirectToAction("Index"); 
       } 
       catch(Exception ex) 
       { 
        alert.Type = AlertType.Error; 
        alert.ExceptionMessage = ex.Message; 

        model = await ViewModel(model.ApplicationUser); 

        ModelState.AddModelError(string.Empty, alert.ExceptionMessage); 
       } 
       return View(model); 
      } 



     private async Task<UserViewModel> ViewModel(ApplicationUser user = null) 
      { 
       var roles = await ApplicationDbContext.Roles.Select(o => new SelectListItem { 
        Value = o.Id.ToString(), 
        Text = o.Name 
       }).ToListAsync(); 

       var viewModel = new UserViewModel 
       { 
        ApplicationUser = (user != null)?user:new ApplicationUser(), 
        Roles = roles,     
       }; 

       return viewModel; 
      } 

     } 
    } 

回答

0

這取決於您當前的界面是如何結構化了一點,但它從你的例子看來你是完全重寫用戶和角色管理器的功能。

我的建議是:

1.查看

  • 有一個多選下拉列表,列出所有角色的,這個用戶是不是在控制器(過濾這些了,成員返回前列表到視圖)編輯GET操作。

您選擇您想要添加用戶的所有人。當您點擊保存/更新時,您將整個ViewModel發回控制器。單獨的Guid屬性不是必需的。

2.控制器

首先,我可以看到在這個例子中一些非常不好的做法。你有控制器內的數據庫操作邏輯,我強烈建議。

嘗試搜索以下主題:

  • 多層架構
  • 服務/ Repository模式

在這方面,你可以重組在解決方案中通過以下方式項目:

  1. Web(Web應用程序項目包含控制器,視圖,視圖模型,a第二應用啓動邏輯)
  2. 服務(業務邏輯)
  3. 庫(你的數據庫訪問邏輯)
  4. DAL(或數據 - 這是數據庫模型,分貝範圍內,遷移,數據庫連接字符串... )

還有一個單獨的模型或核心項目,它包含您需要隨處可用的模型,擴展方法如順序guid等......有很多很好的例子,所以請嘗試搜索它們一點點。

我對你目前的問題的例子是東西的線路:https://dotnetfiddle.net/izWNfm

我刪除了你的樣板代碼把重點放在您遇到的問題,這個例子也可以通過將用戶和角色管理改進通過某種形式的依賴注入。