如何将视图模型列表作为输入参数发送到提交按钮上的视图上的操作单击按钮

时间:2018-09-12 09:36:41

标签: asp.net-mvc

我对MVC非常陌生,我正在努力实现以下目标。

我在其中显示角色下拉列表的视图。选定索引后,此下拉列表将发生更改。我显示页面的名称,并在每个页面名称的前面显示一个复选框。如果角色有权访问该页面,则选中该复选框。 现在,我要编辑角色的权限,方法是选中/取消选中复选框并保存更新按钮单击。

Screen shot of my View

到目前为止,我已经完成了以下工作。

查看模型

public class PagesViewModel
{
    public int PageID { get; set; }
    public string PageTitle { get; set; }
    public string SectionName { get; set; }
    public bool IsSelected { get; set; }
}
 public class RoleViewModel
{
    public int RoleID { get; set; }
    public string RoleName { get; set; }
    public List<PagesViewModel> pages { get; set; }
}

我的控制器

    public ActionResult Index()
    {
        RoleViewModel _RolePriv = new RoleViewModel();
        _RolePriv.RoleID = 0;
        _RolePriv.RoleName = "";
        _RolePriv.pages = GetPages();
        GetRoles(0);
        return View(_RolePriv);
    }

    [HttpPost]
    public ActionResult GetPrivilege(RoleViewModel rolePriv)
    {
        AdminRole role = new AdminRole();
        string joined = string.Join(",", rolePriv.pages.Where(x => 
        x.IsSelected == true).Select(x => x.PageID));
        role = db.AdminRoles.Where(c => c.ID == rolePriv.RoleID).Single();
        role.ID = rolePriv.RoleID;
        role.AdminPrivilege = joined;
        db.AdminRoles.Attach(role);
        //db.Entry(role).State = System.Data.Entity.EntityState.Modified;
        db.SaveChanges();

        return RedirectToAction("Index"); 
    }

    [HttpGet]
    public ActionResult GetPrivilege(int ID)
    {

        GetRoles(ID);
        if (ID != 0)
        {
             RoleViewModel _RolePriv = new RoleViewModel();
            _RolePriv.RoleID = ID;
            _RolePriv.RoleName = (from r in db.AdminRoles
                             where r.ID == ID
                             select r.Name).FirstOrDefault();

            var privilege = (from r in db.AdminRoles
                             where r.ID == ID
                             select r.AdminPrivilege).FirstOrDefault();

        List<int> PageIds = 
       privilege.Split(';').Select(int.Parse).ToList();
      List<PagesViewModel> _menus = db.AdminPages.OrderBy(x => 
      x.ViewOrder)
     .Where(x => x.Parent==0 &&                                                               
      x.PageLevel==0 && 
                                x.Active == 1)
       .Select(x => new PagesViewModel{                                                          
        PageID = x.ID,                                            
        PageTitle = x.Title,                                                                
        SectionName = x.AdminSection.Name,                                                                
        IsSelected = (                                                                
        PageIds.Contains(x.ID) ? true : false)
                                                        }).ToList();

                        _RolePriv.pages = _menus;
                        return View("Index", _RolePriv);
        }

        return RedirectToAction("Index"); 

    }

    public List<PagesViewModel> GetPages()
    {
        List<PagesViewModel> _menus1 = db.AdminPages.OrderBy(x => 
        x.ViewOrder)                                                           
      .Where(x => x.Parent == 0 &&                                                                   
             x.PageLevel == 0 &&                                                                   
             x.Active == 1)                                                           
         .Select(x => new PagesViewModel                                                           
            {                                                               
           PageID = x.ID,                                                               
           PageTitle = x.Title,                                                               
           SectionName = x.AdminSection.Name,                                                                
           IsSelected = false                                                           
           }).ToList();
        return _menus1;
    }


    public void GetRoles(int ID)
    {
        List<SelectListItem> roleList = new List<SelectListItem>();
        roleList = db.AdminRoles.ToList().Select(x => new SelectListItem
        {
            Value = x.ID.ToString(),
            Text = x.Name
        }).ToList();

          roleList.Insert(0, new SelectListItem { Text = "-Select Role-", 
           Value = "0" });

        ViewBag.RoleList = new SelectList(roleList, "Value", "Text", ID);
       }

    }
 }

我的看法

   @model MvcMPark.ViewModel.RoleViewModel
    @{
         ViewBag.Title = "Index";
         Layout = "~/Views/Shared/_Layout.cshtml";
    }

   <p >
       @Html.Label("Edit Privilege")
   </p>

  @using (Html.BeginForm())
  {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)


   @Html.DropDownList("RoleList1", (SelectList)ViewBag.RoleList,new { 
       onchange = "document.location.href = 
      'http://localhost:61628/RolePrivilege/GetPrivilege/' + 
        this.options[this.selectedIndex].value;",  
      style="display:block;"})

@Html.HiddenFor(m => m.RoleID)
@Html.DisplayFor(m => m.RoleName, new { style="diaplay:block;"})


 <ul>
    @for (int i = 0; i < Model.pages.Count; i++)
    {
        <li style="clear:both;">
                @Html.HiddenFor(m => m.pages[i].PageID)
                 @Html.CheckBoxFor(m => m.pages[i].IsSelected, new { 
                 style="float:left;margin:5px;"})
                 @Html.LabelFor(m => m.pages[i].IsSelected, 
                 Model.pages[i].PageTitle, new { style="float:left;"})
         </li>
    }
</ul>


    <p>
        <input type="submit" value="Update"/>
    </p>
   }
  
      
  1. 我希望视图成为
  2.   
Section1
     page1
     page2

Section2
     page3
     page4

但是现在它像这样

  page1
  page2
  page3
  page4

如何以我想要的方式带来它

  
      
  1. 在“提交”按钮上单击时,将调用以下操作,该对象内的值正确,但不会被db.SaveChanges()保存到“角色”中。
  2.   
[HttpPost]
    public ActionResult GetPrivilege(RoleViewModel rolePriv)

那么调用提交按钮单击操作的正确方法是什么,以便调用新函数“ SetPrivilege”而不是GetPrivilege,还有为什么我的值没有保存

0 个答案:

没有答案