我有一个用户列表,如何将其更改为下拉列表(在 MVC 中使用 Razor)?
我使用以下代码获取用户列表:
这是我的视图:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与往常一样,在 ASP.NET MVC 应用程序中,您可以首先设计一个满足视图要求的视图模型(根据您在问题中所描述的内容,显示包含所有用户的下拉列表):
然后编写一个控制器来查询您的存储库并构造一个将传递给视图的视图模型:
最后编写一个相应的视图:
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):
then you write a controller that will query your repositories and construct a view model that will be passed to the view:
and finally you write a corresponding view:
您可以在以下帖子中查看我的答案
使用viewbag将数据绑定到MVC3中partialview的下拉列表
我给出了一个示例如何使用IEnumerable作为模型属性并分配控制器中的值并在视图中使用。(视图使用 Aspx 视图引擎)对于 Razor 尝试以下操作
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
操作方法:
视图:
这是一种简单的、不可扩展的方式,展示了如何使用
DropDownList()
的想法(您也可以使用 <代码>DropDownListFor())。但我建议构建某种 ViewModel 来包含这些数据,然后您将使用强类型视图,并将 ViewModel 作为@model
。Action Method:
View:
This is the simple, unscalable way that shows the idea of how to use
DropDownList()
(you could also useDropDownListFor()
). 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
.