2012-03-23 103 views
1

我是一個初學者在asp mvc 3,我開發了一個應用程序, 我首先想描述我的應用程序 所以我在我的數據庫中,一個表帳戶和一個表角色和關係帳號和角色之間的船是多對多的,所以我有一個關聯表account_role, 我與實體框架數據庫第一工作,我產生我的POCO與EF的DbContext發電機, 我想安安編輯帳戶頁面dispay角色 本的chekbox是我的代碼 控制器帳戶複選框不保留其狀態

public ActionResult Edit(int id) 
     { 
      accounts accounts = db.accounts 
       .Include(i => i.roles_accounts) 
       .Where(i => i.id_account == id) 
       .Single(); 
      PopulateAssignedRoleData(accounts); 
      return View(accounts); 
     } 

     // populate Assigned RoleDATA pour afficher les checkbox 
     private void PopulateAssignedRoleData(accounts account) 
     { 
      //Get all role 
      var allRole =db.roles; 
      //For each role, the code checks if the role exists in the property of accountRole 
      // To create effective search when checking if a role is assigned to the account, 
      // assigned roles in are put into a collection HashSet 
      var accountRoles = new HashSet<int>(account.roles_accounts.Select(r => r.id_account_role)); 
      var viewModel = new List<AssignedRoleData>(); 
      // Property Assigned role of which is allocated account is set to true. 
      //The view will use this property to determine 
      //what check boxes to be displayed as selected. 
      //Finally, the list is passed to the view in a ViewBag 
      foreach (var role in allRole) 
      { 
       viewModel.Add(new AssignedRoleData 
       { 
        RoleId = role.id_role, 
        Name = role.name, 
        Assigned = accountRoles.Contains(role.id_role) 
       }); 
      } 
      ViewBag.roles = viewModel; 
     } 

     // 
     // POST: /Account/Edit/5 

     [HttpPost] 
     public ActionResult Edit(int id, FormCollection formCollection, string [] selectedRoles) 
     { 


      var accountsToUpdate = db.accounts 
       .Include(i => i.roles_accounts) 
       .Where(i => i.id_account == id) 
       .Single(); 
      if (TryUpdateModel(accountsToUpdate, "", null, new string[] { "roles_accounts" })) 
      { 
       try 
       { 
        if (String.IsNullOrWhiteSpace(accountsToUpdate.login)) 
        { 
         accountsToUpdate.roles_accounts = null; 
        } 
        UpdateAccountRole(selectedRoles, accountsToUpdate); 
        db.Entry(accountsToUpdate).State = EntityState.Modified; 
        db.SaveChanges(); 
        return RedirectToAction("Index"); 
       } 

       catch (DataException) 
       { 
        ModelState.AddModelError("", "Unable to save change"); 
       } 
     } 
      PopulateAssignedRoleData(accountsToUpdate); 
      return View(accountsToUpdate); 
    } 

     // update AccountRole (liste of checkbox) 
     private void UpdateAccountRole(string[] selectedRoles, accounts accountToUpdate) 
     { 
      if (selectedRoles == null) 
      { 

       accountToUpdate.roles_accounts=new List<roles_accounts>(); 
       return; 
      } 
      var selectedRolesHS = new HashSet<string>(selectedRoles); 
      var accountsRoles = new HashSet<int> 
      (accountToUpdate.roles_accounts.Select(r => r.id_account_role)); 

      foreach(var role in db.roles_accounts) 
      { 
       if(selectedRolesHS.Contains(role.id_account_role.ToString())) 
       { 
        if(!accountsRoles.Contains(role.id_account_role)) 
        { 
         accountToUpdate.roles_accounts.Add(role); 
        } 
       } 
       else 
       { 
        if (accountsRoles.Contains(role.id_account_role)) 
        { 
         accountToUpdate.roles_accounts.Remove(role); 
        } 
       } 
      } 
     } 

而且我創建一個文件夾nammed的ViewModels,並且此文件夾中我創建了一個CLASSE AssignedRoleData要爲複選框列表提供的數據來看, 這是AssignedRoleData

public class AssignedRoleData 
    { 
     public int RoleId { get; set; } 
     public string Name { get; set; } 
     public bool Assigned { get; set; } 

,並在Edit.schtml 我把這個代碼

<div class="editor-field"> 
    <table> 
     <tr> 
      @{ 
       int cnt = 0; 

       List<App_ERP1.ViewModels.AssignedRoleData> roles=ViewBag.roles; 

       foreach (var role in roles) { 
        if (cnt++ % 3 == 0) { 
         @: </tr> <tr> 
        } 
        @: <td> 
         <input type="checkbox" 
           name="selectedRoles" 
           value="@role.RoleId" 
           @(Html.Raw(role.Assigned ? "checked=\"checked\"" : "")) /> 
         @role.RoleId @: @role.Name 
        @:</td> 
       } 
       @: </tr> 
      } 

    } 
} 

我的問題是該複選框不保持其狀態,並每次當我點擊保存按鈕,它刪除添加的角色(選擇)

感謝幫助我

回答

0

您需要在視圖中使用「for」而不是「foreach」,並且您的屬性「name」可能需要像name =「selectedRoles [i]」。提示:請勿直接在控制器上訪問數據庫。創建一個具有AccountRoleService類的中間層,在這裏放置邏輯,然後創建另一個用於訪問數據庫的層(例如AccountRoleRepository),您可以在其中實際執行LINQ to SQL內容。如果你想重用方法會容易得多。因此,控制器幾乎不做任何調用服務類。服務類執行邏輯並調用存儲庫類來訪問數據庫。所以如果你想單元測試服務,你將會走在正確的道路上。

+0

感謝您的快速回答,我不知道如何改變我的看法,我使用了foreach,因爲我必須瀏覽類AssignedRoleData中的角色,請您解釋您的觀點,知道我初學者在asp mvc 3 – julia 2012-03-23 21:22:34

+0

你能給我一個解決方案,因爲3天,我試圖找到一個解決方案,但我在同樣的問題 – julia 2012-03-26 10:58:38

0
for (i = 0; i< roles.Count, i++) { 
        if (cnt++ % 3 == 0) { 
         @: </tr> <tr> 
        } 
        @: <td> 
         <input type="checkbox" 
           name="selectedRoles[" + i + "]" 
           value="@role.RoleId" 
           @(Html.Raw(role.Assigned ? "checked=\"checked\"" : "")) /> 
         @role.RoleId @: @role.Name 
        @:</td> 
       } 
       @: </tr>