ASP.Net MVC ViewBag在View中不起作用。 ViewBag结果正确

时间:2020-02-10 22:06:10

标签: c# asp.net-mvc razor-pages

我在ViewBag中遇到问题,无法正常工作。控制器返回我认为我需要的东西。

在调试时,我有Current = false,然后在结果视图中,我也有false。该位设置为false。 似乎所有ViewBag项的Current返回False。

当我在视图中调用ViewBag时,它似乎没有任何作用。它也没有错。

这是我的代码部分,我从数组中获取当前的布尔信息。

 if (GetOrPost == "GET")
 {
    if (actionName == "add" || actionName == "index" || actionName == "create" || actionName == "edit" || actionName == "delete" || actionName == "multiviewindex")
    {
      ViewBag.Add = menuaccess.Where(i => i.MenuURL == controllerName).Select(i => (i.IsAdd));
      ViewBag.Read = menuaccess.Where(i => i.MenuURL == controllerName).Select(i => (i.IsRead));
      ViewBag.Create = menuaccess.Where(i => i.MenuURL == controllerName).Select(i => (i.IsCreate));
      ViewBag.Edit = menuaccess.Where(i => i.MenuURL == controllerName).Select(i => (i.IsUpdate));
      ViewBag.Delete = menuaccess.Where(i => i.MenuURL == controllerName).Select(i => (i.IsDelete));
    }
  }

这是视图中的用法:

  @if (ViewBag.Edit = true)
  {
     @Ajax.ModalDialogActionLink("Edit", "Edit", "Edit User", "btn btn-warning btn-sm", new { UserName = item.UserName })
  }

MenuOfRole会带回已登录的RoleUser的所有权限。根据控制器和操作,我可以提取自己所需的位置。

关于为什么它在视图中不起作用的任何想法。而且,如果有更清洁,更轻松的方法可以做到这一点-我很高兴!

谢谢

更新: 我在视图中进行了检查-仅返回ViewBag,它又返回了true,但是在Controller中它说这是假的。我不确定为什么会这样。如果在Controller中将ViewBag设置为false,它应该在视图中显示为false,不是吗?

编辑:按要求,这是我的ForEach循环。

<tbody>
   @foreach (var item in Model)
   {
     <tr>
        <td>
            @Html.DisplayFor(modelItem => item.UserName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Role)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LockoutEndDateUtc)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CompanyName)
        </td>
        <td>

          @if (ViewBag.Edit == true)
          {
             @Ajax.ModalDialogActionLink("Edit", "Edit", "Edit User", "btn btn-warning btn-sm", new { UserName = item.UserName })
          }
          @if (User.IsInRole("Administrator"))
          {
             @Ajax.ModalDialogActionLink("PW Reset", "SendResetPassword", "Account", "Reset Password", "btn btn-info btn-sm", new { UserName = item.UserName })
             <text>&nbsp;</text>
             @Ajax.ModalDialogActionLink("Conf E-Mail", "SendEmailConfirm", "Account", "Email Confirmation", "btn btn-info btn-sm", new { UserName = item.UserName })
          }
          @if ((item.UserName.ToLower() != this.User.Identity.Name.ToLower()))
          {
            if (ViewBag.Delete == true)
            {
               @Html.ActionLink("Delete", "Delete", null, new { UserName = item.UserName },
               new { onclick = "return Confirm('Are you sure you want to delete this user?');", @class = "btn btn-danger btn-sm" })
             }
             <div class="btn-group">
                 @Html.ActionLinkAuthorized("Edit Roles", "Edit", "Roles", new { UserName = item.UserName }, new { @class = "btn btn-warning btn-sm" }, true)
             </div>
         }
        </td>
     </tr>
    }
 </tbody>

更新:有关此问题的完整答案,请转到本文。

stackoverflow.com/questions/60173428/

1 个答案:

答案 0 :(得分:0)

以下行返回布尔值的集合:

menuaccess.Where(i => i.MenuURL == controllerName).Select(i => (i.IsUpdate));

因此,ViewBag.Edit(对分配给ViewBag的其他对象的解释相同)

这就是@if (ViewBag.Edit = true)评估不正确的原因。

要解决此问题,请修改代码部分以使用FirstOrDefault()从控制器中的集合中仅返回一个元素,如下所示:

if (GetOrPost == "GET")
 {
    if (actionName == "add" || actionName == "index" || actionName == "create" || actionName == "edit" || actionName == "delete" || actionName == "multiviewindex")
    {
      ViewBag.Add = menuaccess.Where(i => i.MenuURL == controllerName).Select(i => (i.IsAdd)).FirstOrDefault();
      ViewBag.Read = menuaccess.Where(i => i.MenuURL == controllerName).Select(i => (i.IsRead)).FirstOrDefault();
      ViewBag.Create = menuaccess.Where(i => i.MenuURL == controllerName).Select(i => (i.IsCreate)).FirstOrDefault();
      ViewBag.Edit = menuaccess.Where(i => i.MenuURL == controllerName).Select(i => (i.IsUpdate)).FirstOrDefault();
      ViewBag.Delete = menuaccess.Where(i => i.MenuURL == controllerName).Select(i => (i.IsDelete)).FirstOrDefault();
    }
  }

请注意,我建议使用FirstOrDefault(),前提是集合将包含相同的值。我的意思是相同的值,是集合包含全部为true或全部为false,但不包含true和false的混合。

如果您的集合同时包含true和false,则需要使用Any,如下所示:

if (GetOrPost == "GET")
{
    if (actionName == "add" || actionName == "index" || actionName == "create" || actionName == "edit" || actionName == "delete" || actionName == "multiviewindex")
    {
      ViewBag.Add = menuaccess.Any(i => i.MenuURL == controllerName && i.IsAdd);
      ViewBag.Read = menuaccess.Any(i => i.MenuURL == controllerName && i.IsRead);
      ViewBag.Create = menuaccess.Any(i => i.MenuURL == controllerName && i.IsCreate);
      // Any will be evaluated to true if any of item's MenuURL is equal to controllerName and IsUpdate set to true. false will be returned otherwise. Same explanation for others as well.
      ViewBag.Edit = menuaccess.Any(i => i.MenuURL == controllerName && i.IsUpdate);
      ViewBag.Delete = menuaccess.Any(i => i.MenuURL == controllerName && i.IsDelete);
    }
}
相关问题