相关表不保存在MVC应用程序

本文关键字:存在 MVC 应用程序 保存 | 更新日期: 2023-09-27 18:17:53

我有以下模型-

public class RoleModel
{
    public int Id { get; set; }
    public string RoleName { get; set; }
    public string Description { get; set; }
    public DateTime DateCreated { get; set; }
    public int CreatedBy { get; set; }
    public DateTime LastUpdated { get; set; }
    public int LastUpdateBy { get; set; }
    [NotMapped]
    public State State { get; set; }
    public virtual IEnumerable<UserModel> Users { get; set; }
    public virtual IEnumerable<UserModel> UsersNotInRole { get; set; }
    public virtual int[] SelectedUsers { get; set; }
    public virtual List<RightModel> Rights { get; set; }
    public virtual List<RightModel> SelectedRights { get; set; }
    public RoleModel()
    {
    }
}
public class RightModel
{
    public string RightName { get; set; }
    public string Description { get; set; }
    public bool Assigned { get; set; }
}

由此,每个角色都有分配给它们的权限集合。在下面的视图中,我希望允许用户针对他们想要分配给所选角色的每个权限选择一个复选框。视图正在正确加载数据并检查正确的框,但是当我按下"Save"时,Rights列表为空。我该如何纠正这一点,以便它从所选角色中删除所有权限,然后重新分配所需的权限。

 @model Project.Core.Models.Roles.RoleModel
 @using (Html.BeginForm())
 {
      @Html.ValidationSummary(true)
      <div class="tab-pane" id="tab_1_3">
                                    <table class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th>Right Name</th>
                                                <th>Description</th>
                                                <th></th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach (var right in Model.Rights)
                                            {
                                                <tr>
                                                    <td>@Html.DisplayFor(model => right.RightName)</td>
                                                    <td>@Html.DisplayFor(model => right.Description)</td>
                                                    <td>
                                                        <div class="success-toggle-button">
                                                            @Html.CheckBoxFor(model => right.Assigned, new { @class = "toggle" })
                                                        </div>
                                                    </td>
                                                </tr>
                                            }
                                        </tbody>
                                    </table>
                                </div>

and my controller -

[Authorize]
    public ActionResult Details(int id = 0)
    {
        RoleModel role = _roleService.GetById(id);
        if (role == null)
        {
            return HttpNotFound();
        }
        return View(role);
    }
    [HttpPost]
    public ActionResult Details(RoleModel model)
    {
        if (ModelState.IsValid)
        {
            _roleService.Update(model);
            return RedirectToAction("Index");
        }
        return View(model);
    }

和我的更新方法在RoleService -

public void Update(RoleModel entity)
    {
        entity.LastUpdated = DateTime.Now;
        entity.LastUpdateBy = 1;
        Role r = _roleRepository.FindById(entity.Id);
        AutoMapper.Mapper.CreateMap<RoleModel, Role>();
        _roleRepository.Update(AutoMapper.Mapper.Map(entity, r));
    }

和我的仓库更新方法-

 public void Update(Role role)
    {
        _context.ObjectStateManager.ChangeObjectState(role, EntityState.Modified);
        SaveChanges();
    }

相关表不保存在MVC应用程序

正如user3153169在评论中所说,对于集合,您需要将元素的id/名称设置为RoleModel.Rights[i]。分配给自动装配器查找元素。

所以你应该像这样使用for循环

@for (int i = 0 ; i < Model.Rights.Count() ; i++)
{
    <tr>
        <td>@Html.DisplayFor(model => Model.Rights[i].RightName)</td>
        <td>@Html.DisplayFor(model => Model.Rights[i].Description)</td>
        <td>
            <div class="success-toggle-button">
                @Html.CheckBoxFor(model => Model.Rights[i].Assigned, new { @class = "toggle" })
            </div>
        </td>
    </tr>
}