1

I'm trying to create a page where a listbox will contain all my ASP.NET users.

However I'm having a lot of problems attempting to make the Html.Listbox accept the list of users.

I have tried doing this:

<%: Html.ListBox("Membershipusers", Membership.GetAllUsers())%>

and a zillion other approaches that is similar to this. I also have a method on my controller that looks like this:

    public ActionResult getAllMembershipUsers()
    {
        MembershipUserCollection membershipusers = new MembershipUserCollection();

        membershipusers = Membership.GetAllUsers();

        return View(membershipusers);
    }

It seems like it is able to create the list of users, but I still don't know how to show that list in my listbox.

Hope somebody can help me out here.

1 Answer 1

2

How about using a view model? Have you tried this approach?

public class UsersViewModel
{
    public IEnumerable<string> SelectedUsers { get; set; }
    public IEnumerable<SelectListItem> AvailableUsers { get; set; }
}

and then a controller which will populate this view model:

public ActionResult Index()
{
    var users = Membership.GetAllUsers().Cast<MembershipUser>();
    var model = new UsersViewModel
    {
        AvailableUsers = users.Select(u => new SelectListItem
        {
            Value = u.UserName,
            Text = u.UserName
        })
    };

    return View(model);
}

and finally in your strongly typed view to the view model

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<UsersViewModel>" %>
...
<%= Html.ListBoxFor(x => x.SelectedUsers, Model.AvailableUsers) %>
Sign up to request clarification or add additional context in comments.

5 Comments

I have used the approach you suggested. it seems to work fine except that i get this error: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[SkyLearn.Areas.Users.Models.User]', but this dictionary requires a model item of type 'SkyLearn.Areas.Users.Models.UsersViewModel'.
@AronChan, the error message seems pretty self explanatory. Your view is strongly typed to List<User> and you are passing a UsersViewModel to it. So you need to adapt your view model.
care to elaborate a bit Darin? not entirely sure how i should change my model
@AronChan, it's very hard to elaborate on how you should change your model without me knowing how your model currently looks like. But you could add a property on the view model that is of type List<User> that will contain the data that you currently use as model.
Hi.. since i changed so much in my way of doing this i created a new question here on stack in order to make the topic reflect what i am actually trying to achieve. it can be found here: stackoverflow.com/questions/10147129/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.