i have the following Action method:-
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AssignGroup(int SecurityRoleID, int[] selectedGroups, int[] currentGroups)
{try
{if (ModelState.IsValid)
{ repository.AssignGroupRole(SecurityRoleID, selectedGroups, currentGroups);
repository.Save();
if (!Request.IsAjaxRequest())
{
return RedirectToAction("AssignGroup", new { id = SecurityRoleID });
}
else if (Request.IsAjaxRequest())
{
ViewBag.Users = repository.populateAssignedGroupData(SecurityRoleID);
return PartialView("_AssignGroup", repository.FindAllRole(SecurityRoleID));
}}}
And the following Partial view:-
@using (Html.BeginForm("AssignGroup", "SecurityRole", FormMethod.Post))
{
<span class="b">There are Currently @Model.Groups.Count() Group/s</span>
<table class="table table-striped table-bordered bootstrap-datatable datatable">
<thead>
<tr>
<th> @Html.DisplayNameFor(model => model.Groups.SingleOrDefault().Name)</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
@{
int cnt = 0;
List<TMS.ViewModels.GroupAssign> groups = ViewBag.Groups;
foreach (var group in groups.OrderBy(a=>a.Name)) {
if (cnt++ % 5 == 0) {
@: </tr> <tr>
}
@: <td>
<input type="checkbox"
name="selectedGroups"
value="@group.GroupId"
@(Html.Raw(group.IsChecked ? "checked=\"checked\"" : "")) />
@: @group.Name
@Html.Hidden("currentGroups", group.GroupId)
@:</td>
}
@: </tr>
}
</tbody>
@Html.HiddenFor(model => model.SecurityRoleID)
@Html.AntiForgeryToken()
</table>
<input type="submit" value="Assign Groups" class="btn btn-primary"/>}
and the following repository methods:-
public List<GroupAssign> populateAssignedGroupData(int SecurityRoleID)
{
var allGroups = AllGroup();
var securityRole = FindAllRole(SecurityRoleID);
var roleGroups = securityRole.Groups.Select(a => a.Name);
var viewModel = new List<GroupAssign>();
foreach (var group in allGroups)
{
viewModel.Add(new GroupAssign
{
GroupId = group.GroupID,
Name = group.Name,
IsChecked = roleGroups.Contains(group.Name, StringComparer.OrdinalIgnoreCase)
});
}
return viewModel;
}
public void AssignGroupRole(int id, int[] selectedGroups, int[] currentGroups)
{
var roleGroups = FindRole(id).Groups;
var securityRole = FindAllRole(id);
foreach (var group in roleGroups.ToList())
{
if (currentGroups != null)
{
for (int c = 0; c < currentGroups.Count(); c++)
{
if (group.GroupID == currentGroups[c])
{
securityRole.Groups.Remove(group);} }} }
if (selectedGroups != null)
{
for (int i = 0; i < selectedGroups.Count(); i++)
{
Group r = new Group();
r.GroupID = selectedGroups[i];
securityRole.Groups.Add(r);}}}
But whne i click on the "Assign Groups" button inside the view i will get the following error on the code foreach (var group in groups.OrderBy(a=>a.Name))
insdie the partial view:-
ystem.ArgumentNullException was unhandled by user code
HResult=-2147467261 Message=Value cannot be null. Parameter name: source Source=System.Core ParamName=source StackTrace: at System.Linq.OrderedEnumerable2..ctor(IEnumerable
1 source, Func2 keySelector, IComparer
1 comparer, Boolean descending) at System.Linq.Enumerable.OrderBy[TSource,TKey](IEnumerable1 source, Func
2 keySelector) at ASP._Page_Views_SecurityRole__AssignGroup_cshtml.Execute() in c:\Users\Administrator\Documents\Visual Studio 2012\Projects\TMS\TMS\Views\SecurityRole_AssignGroup.cshtml:line 56 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.<>c_DisplayClass1a.b_17() at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
InnerException: