1

I am getting a list of my users using the below code:

Here is my view:

@model System.Web.Security.MembershipUserCollection

@{
    ViewBag.Title = "ManageProfile";
}
<div class ="hero-unit">
<h2>ManageProfile</h2>

<ul>
       @foreach (MembershipUser user in Model)
       {<li>@user.UserName</li>}

</ul>
</div>

Here is my controller:

    public ActionResult ManageProfile()
    {
        var users = Membership.GetAllUsers();
        return View(users);
    }

I would prefer that I be able to select the user from a dropdown list. How do I modify my code to yield a dropdown list? (my goal is to be able to select the user from the dropdown list and change details of that user's profile)

Ecnalyr
  • 5,792
  • 5
  • 43
  • 89

3 Answers3

1

As always in ASP.NET MVC application you could start by designing a view model that will meet the requirements of your view (which from what you described in your question is displaying a dropdownlist with all users):

public class MyViewModel
{
    [Display(Name = "select user")]
    public string SelectedUser { get; set; }
    public IEnumerable<SelectListItem> Users { get; set; }
}

then you write a controller that will query your repositories and construct a view model that will be passed to the view:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var users = Membership.GetAllUsers();
        var model = new MyViewModel
        {
            Users = users.OfType<MembershipUser>().Select(x => new SelectListItem
            {
                Value = x.UserName,
                Text = x.UserName
            })
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(string selectedUser)
    {
        return Content("Thank you for selecting " + selectedUser);
    }
}

and finally you write a corresponding view:

@model MyViewModel
@using (Html.BeginForm())
{
    @Html.LabelFor(x => x.SelectedUser)
    @Html.DropDownListFor(x => x.SelectedUser, Model.Users)
    <button type="submit">OK</button>
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 2
    @moguzalp, for me personally a view model is always practical. Even in small projects. Well, depends on your definition of small project of course. – Darin Dimitrov Feb 22 '12 at 18:45
  • @moguzalp I disagree with that. ViewModels alleviate maintenance and maximize scalability. –  Feb 22 '12 at 18:45
  • This was a very clean solution for what I needed. Thank you for the succinct reply and detailed explanation, actually (inadvertently) taught me something very specific and useful for another part of my project as well. – Ecnalyr Feb 22 '12 at 18:45
0

You can have a look into my answer in following post

bind data to dropdownlist of partialview in MVC3 using viewbag

I have given an example how to use IEnumerable as a model property and assign values in controller and use in the view.(View uses Aspx View engine) for Razor try following

@Html.DropDownListFor(m=>m.SelectedCountry,Model.countriesList)
@Html.DropDownListFor(m=>m.SelectedState,Model.stateList)
Community
  • 1
  • 1
Manas
  • 2,534
  • 20
  • 21
  • Seemed a little less elegant than what I was able to smoothly learn from, and Darin Dimitrov handled it so wonderfully. Thank you for the assistance though. – Ecnalyr Feb 22 '12 at 18:47
0

Action Method:

public ActionResult ManageProfile()
{
    List<SelectListItem> userNames = new List<SelectListItem>();
    var users = Membership.GetAllUsers();

    foreach (MembershipUser user in users)
        userNames.Add(new SelectListItem() { Text = user.UserName, Value = user.UserName);

    return View(userNames);
}

View:

@model List<SelectListItem>

@{
    ViewBag.Title = "ManageProfile";
}
<div class ="hero-unit">
<h2>ManageProfile</h2>

@Html.DropDownList("SelectedUser", Model)

</div>

This is the simple, unscalable way that shows the idea of how to use DropDownList() (you could also use DropDownListFor()). But I'd recommend constructing some sort of ViewModel to include this data, and then you would use a strongly-typed View with the ViewModel as the @model.