I am very new to MVC, and I am trying to achieve the following.
I have view where I am displaying a dropdown list for roles. On selected index change of this drop-down list. I display the name of pages and a check box in front of each page name. The checkboxes are checked if the role has permission to access that page. Now I want to edit the permission the Role has, by checking/unchecking the checkboxes and saving on update button click.
So far, I have done the following.
View Model
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; }
}
My Controller
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);
}
}
}
My View
@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>
}
- I want the view to be
Section1
page1
page2
Section2
page3
page4
but now it is coming like this
page1
page2
page3
page4
How to bring it in the way I want
- On submit button click the below action is called, the values inside the object is correct but it does not get saved to the Role by db.SaveChanges();
[HttpPost]
public ActionResult GetPrivilege(RoleViewModel rolePriv)
So what is the correct way to call an action on submit button click so that a new function “SetPrivilege” is called and not GetPrivilege and also why are my values not getting saved