使用asp net mvc在用户管理系统中激活/取消激活用户

时间:2016-05-23 09:47:57

标签: c# asp.net user-management

我的帐户控制器中有这个:

public ActionResult UserActivation(string UserName)
{
    setLanguage();
    MembershipUser user = Membership.GetUser(UserName);

    if (user != null)
    {
        if (user.IsApproved == true)
        {
            user.IsApproved = false;
            Membership.UpdateUser(user);
            ViewBag.IsActive = user.IsApproved;
            ViewBag.activationLink = "Activate";
        }
        else
        {
            user.IsApproved = true;
            Membership.UpdateUser(user);
            ViewBag.IsActive = user.IsApproved;
            ViewBag.activationLink = "Deactivate";
        }
    }
    using (var context = new UsersAndRolesModel())
    {
        var users = context.aspnet_Users.OrderBy(x => x.UserName).ToList();
        var approved = from u in context.aspnet_Users
                       join m in context.aspnet_Membership on u.UserId equals m.UserId
                       select new { u.UserName, m.IsApproved };
        ViewBag.Users = users;
        ViewBag.IsApproved = approved.AsQueryable().ToList();
    }
    return View("ManageUsers");
}

在我看来,我称之为: @ViewBag.IsApproved

,浏览器中的结果如下:

System.Collections.Generic.List`1[<>f__AnonymousType5`2[System.String,System.Boolean]]

如何为每个用户获取true或false(分别为每个用户)

1 个答案:

答案 0 :(得分:0)

看起来您正在返回用户列表。

您必须在视图中使用@model IEnumerable<MembershipUser>,并重复检查以获得每个用户的IsApproved。