繁体   English   中英

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

[英]ASP.Net MVC ViewBag is not working in View. ViewBag result is Correct

我遇到了 ViewBag 无法在视图中工作的问题。 控制器返回我认为我需要的东西..

在调试时,我有 Current = 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 并且它恢复为真,但在控制器中它说它是假的。 我不知道为什么会这样。 如果 ViewBag 在 Controller 中设置为 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/

以下行返回布尔值集合:

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,那么您需要使用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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM