ASP.NET MVC 列出所有用户

发布于 2024-07-20 20:31:50 字数 170 浏览 4 评论 0原文

我试图显示所有用户的列表,但不确定如何使用 MVC 模型来实现此目的。

我可以通过 Membership.GetAllUsers() 方法获取所有用户的列表,但是如果我尝试将其从 ActionResult 传递给视图,我会被告知 Model是不可枚举的。

I'm trying to show a list of all users but am unsure how to go about this using the MVC model.

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.

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

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

发布评论

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

评论(5

写给空气的情书 2024-07-27 20:31:50

您必须将视图设置为

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MembershipUserCollection>" %>

在您的操作中接受 MembershipUserCollection 类型的对象:

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

然后您可以在视图中编写如下内容:

 <ul>
       <%foreach (MembershipUser user in Model){ %>

       <li><%=user.UserName %></li>

       <% }%>
</ul>

You have to set the View to accept an object of type 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>
长不大的小祸害 2024-07-27 20:31:50

在视图页面的顶部,您需要设置视图页面的类型。 IE:

在视图的顶部,在标记的第一行中,您将看到类似这样的内容:

Inherits="System.Web.Mvc.ViewPage"

将其更改为:

Inherits="System.Web.Mvc.ViewPage<MembershipUserCollection>"

或您尝试传递给视图的任何类型。 “Model”对象现在将是 MembershipUserCollection 类型,您可以安全地对其进行迭代。

In your view page, on top, you need to set the type of the view page. 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.

远昼 2024-07-27 20:31:50

[编辑] 具体来说,视图是什么样的(例如,它期望获得什么模型,如何解析集合等)

您可以发布一些代码吗? 我正在做类似的事情,但没有任何问题。

[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.

天气好吗我好吗 2024-07-27 20:31:50

听起来您需要使您的视图成为强类型视图。 让您的视图派生自 ViewPage; 而不仅仅是 ViewPage。 另一个解决方案是将模型转换为视图中的 MembershipUserCollection:

<% var members = (MembershipUserCollection) ViewData.Model %>
<% foreach (MembershipUser user in members) { %>
       <%-- Do Stuff with the user --%>
<% } %>

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:

<% var members = (MembershipUserCollection) ViewData.Model %>
<% foreach (MembershipUser user in members) { %>
       <%-- Do Stuff with the user --%>
<% } %>
×纯※雪 2024-07-27 20:31:50

尝试将 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()+" | ";

添加到应用程序对象代码中

        Application["users"] = users;

,从应用程序对象中检索

        string[] usersTable = (string[])Application["users"];

这将帮助您

Try asp.net application object

        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

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