asp.net-mvc - how do i create a view to show all unapproved users and allow them to be approved

StackOverflow https://stackoverflow.com/questions/1212497

  •  06-07-2019
  •  | 
  •  

Question

i have this code in my membership service class (taken from the asp.net-mvc sample app)

  public MembershipUserCollection GetUnapprovedUsers()
    {
        MembershipUserCollection users = Membership.GetAllUsers();
        MembershipUserCollection unapprovedUsers = new MembershipUserCollection();
        foreach (MembershipUser u in users)
        {
            if (!u.IsApproved)
            {
                unapprovedUsers.Add(u);
            }
        }
        return unapprovedUsers;
    }

i now need a view to show this list of information and allow someone to approve them which will go back to the controller and set the IsApproved property to true.

Was it helpful?

Solution

Create a view which will generate a form containing label and checkbox for each member of the collection. You need to be able to get from the id of the checkbox to the user.

In the HTTP.POST Action method, iterate through the submitted fields looking for set checkboxes, when you find one set the corresponding user to approved.

Obviously the form can display arbitrary details for each user.

To use the inbuilt control helpers takes a bit more effort because you don't have a fixed size model to work with. To achieve something similar I:

  • Used a non-strongly typed view
  • populated ViewData["ids"] with IEnumerable<IdType> (which the view would loop over)
  • For each entry populated ViewData["field" + id] for each field I was displaying in the entity
  • In the view looped over the ids using ViewData["ids"] to call the HTML helpers with the id of the field.

(That was V1, in V2 I used model state so I could use the inbuilt validation error display support, but that doesn't really apply if you just want to select users.)

The POST processing was similar, repopulating the id list from the database and the looking up in the passed FormCollection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top