C# ASP.NET MVC 列出所有用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/874791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC List All Users
提问by Jamie Dixon
I'm trying to show a list of all users but am unsure how to go about this using the MVC model.
我正在尝试显示所有用户的列表,但不确定如何使用 MVC 模型进行处理。
I can obtain the list of all users via the Membership.GetAllUsers()
method however if I try to pass this to the view from the ActionResult
, I'm told that Model is not enumerable.
我可以通过该Membership.GetAllUsers()
方法获取所有用户的列表,但是如果我尝试将其从 传递给视图ActionResult
,我会被告知 Model 不可枚举。
采纳答案by BFree
You have to set the View to accept an object of type MembershipUserCollection
您必须将视图设置为接受 MembershipUserCollection 类型的对象
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MembershipUserCollection>" %>
In your action:
在你的行动中:
public ActionResult GetUsers()
{
var users = Membership.GetAllUsers();
return View(users);
}
then you can write in your view something like:
那么你可以在你的视图中写下类似的东西:
<ul>
<%foreach (MembershipUser user in Model){ %>
<li><%=user.UserName %></li>
<% }%>
</ul>
回答by nkirkes
[Edit] Specifically, what does the view look like (e.g. what's it expecting to get as the model, how are you parsing the collection, etc.)
[编辑] 具体来说,视图是什么样子的(例如它期望得到什么作为模型,你如何解析集合等)
Can you post some code? I'm doing something similar, but am having no problems.
你可以发布一些代码吗?我正在做类似的事情,但没有问题。
回答by BFree
In your view page, on top, you need to set the type of the view page. IE:
在您的查看页面中,在顶部,您需要设置查看页面的类型。IE:
On the top of your View, in the first line of the markup, you'll see something like this:
在您的视图顶部,在标记的第一行,您将看到如下内容:
Inherits="System.Web.Mvc.ViewPage"
Change that to be:
将其更改为:
Inherits="System.Web.Mvc.ViewPage<MembershipUserCollection>"
or whatever the type you're trying to pass to the view. The "Model" object will now be of type MembershipUserCollection which you can safely iterate over.
或者您尝试传递给视图的任何类型。“模型”对象现在将是 MembershipUserCollection 类型,您可以安全地对其进行迭代。
回答by Anthony Conyers
It sounds like you need to make your view strongly typed view. Have your view derive from ViewPage<MembershipUserCollection>
instead of just ViewPage
. Another solution is to cast your Model to MembershipUserCollection in your view:
听起来您需要使您的视图成为强类型视图。让您的观点来自ViewPage<MembershipUserCollection>
而不仅仅是ViewPage
。另一种解决方案是在您的视图中将您的模型转换为 MembershipUserCollection:
<% var members = (MembershipUserCollection) ViewData.Model %>
<% foreach (MembershipUser user in members) { %>
<%-- Do Stuff with the user --%>
<% } %>
回答by Sajith Dushmantha Samarathunga
Try asp.net application object
尝试 asp.net 应用程序对象
string id = Session.SessionID.ToString();
String[] users = new String[1000];
users = (string[])Application["users"];
int count=0;
for (int d=0;1000 >d ;d++ )
{
if (users == null) { users = new String[1000]; }
count = d;
if (users[d] == null) { break; }
}
users[count] = id;
Application["users"] = users;
string[] usersTable = (string[])Application["users"];
for (int d=0;1000 >d ;d++ )
{
if (usersTable[d] == null) { break; }
Label1.Text += usersTable[d].ToString()+" | ";
add to application object code
添加到应用程序对象代码
Application["users"] = users;
retrive from applicaton object
从应用程序对象中检索
string[] usersTable = (string[])Application["users"];
this will help you
这会帮助你