0

我有以下操作方法:-

    [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));
                        }}}

以及以下部分视图:-

@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"/>}

以及以下存储库方法:-

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);}}}

但是,当我单击视图内的“分配组”按钮时,我将 foreach (var group in groups.OrderBy(a=>a.Name))在部分视图中的代码上收到以下错误:-

ystem.ArgumentNullException 未被用户代码处理
HResult=-2147467261 Message=Value 不能为空。参数名称:source Source=System.Core ParamName=source StackTrace:在 System.Linq.OrderedEnumerable 2..ctor(IEnumerable1 源,Func 2 keySelector, IComparer1 比较器,布尔降序)在 System.Linq.Enumerable.OrderBy[TSource,TKey](IEnumerable1 source, Func2 keySelector)在 c:\Users\Administrator\Documents\Visual Studio 2012\Projects\TMS\TMS\Views\SecurityRole_AssignGroup.cshtml 中的 ASP._Page_Views_SecurityRole__AssignGroup_cshtml.Execute():System.Web.WebPages.WebPageBase.ExecutePageHierarchy 的第 56 行( ) 在 System.Web.Mvc.WebViewPage.ExecutePageHierarchy() 在 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) 在 System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer,对象实例)在 System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) 在 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) 在 System.Web.Mvc.ControllerActionInvoker。InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) 在 System.Web.Mvc.ControllerActionInvoker.<>c_DisplayClass1a.b _17() 在 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter 过滤器,ResultExecutingContext preContext,Func`1 延续)
InnerException:

4

1 回答 1

1

异常意味着 Linq 查询 ( groups) 的来源为空。您需要确保ViewBag.Groups已初始化(这不会在您发布的代码中的任何地方发生)。

你想要Model.Groups吗?

于 2013-07-20T17:23:53.223 回答