我是一个初学者在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>
}
}
}
我的问题是该复选框不保持其状态,并每次当我点击保存按钮,它删除添加的角色(选择)
感谢帮助我
感谢您的快速回答,我不知道如何改变我的看法,我使用了foreach,因为我必须浏览类AssignedRoleData中的角色,请您解释您的观点,知道我初学者在asp mvc 3 – julia 2012-03-23 21:22:34
你能给我一个解决方案,因为3天,我试图找到一个解决方案,但我在同样的问题 – julia 2012-03-26 10:58:38