views:

177

answers:

1

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.

+1  A: 

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.

Richard
the question is how do i get the view to request this from the controller. do i simply stick this in ViewData
ooo
Yes (or ModelData, albeit that is harder to use).
Richard
@oo: The View should not request anything from anywhere. The View should be **given** all the data it requires.
JoshJordan

related questions