我有一个用户列表,如何将其更改为下拉列表(在 MVC 中使用 Razor)?

发布于 2025-01-08 07:10:21 字数 599 浏览 1 评论 0原文

我使用以下代码获取用户列表:

这是我的视图:

@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>

这是我的控制器:

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

我希望能够从下拉列表中选择用户。如何修改我的代码以生成下拉列表? (我的目标是能够从下拉列表中选择用户并更改该用户个人资料的详细信息)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绳情 2025-01-15 07:10:21

与往常一样,在 ASP.NET MVC 应用程序中,您可以首先设计一个满足视图要求的视图模型(根据您在问题中所描述的内容,显示包含所有用户的下拉列表):

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

然后编写一个控制器来查询您的存储库并构造一个将传递给视图的视图模型:

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);
    }
}

最后编写一个相应的视图:

@model MyViewModel
@using (Html.BeginForm())
{
    @Html.LabelFor(x => x.SelectedUser)
    @Html.DropDownListFor(x => x.SelectedUser, Model.Users)
    <button type="submit">OK</button>
}

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>
}
寄居人 2025-01-15 07:10:21

您可以在以下帖子中查看我的答案

使用viewbag将数据绑定到MVC3中partialview的下拉列表

我给出了一个示例如何使用IEnumerable作为模型属性并分配控制器中的值并在视图中使用。(视图使用 Aspx 视图引擎)对于 Razor 尝试以下操作

@Html.DropDownListFor(m=>m.SelectedCountry,Model.countriesList)
@Html.DropDownListFor(m=>m.SelectedState,Model.stateList)

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)
浅忆流年 2025-01-15 07:10:21

操作方法

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);
}

视图

@model List<SelectListItem>

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

@Html.DropDownList("SelectedUser", Model)

</div>

这是一种简单的、不可扩展的方式,展示了如何使用 DropDownList() 的想法(您也可以使用 <代码>DropDownListFor())。但我建议构建某种 ViewModel 来包含这些数据,然后您将使用强类型视图,并将 ViewModel 作为 @model

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文