ASP.NET MVC 列出所有用户
我试图显示所有用户的列表,但不确定如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您必须将视图设置为
在您的操作中接受 MembershipUserCollection 类型的对象:
然后您可以在视图中编写如下内容:
You have to set the View to accept an object of type MembershipUserCollection
In your action:
then you can write in your view something like:
在视图页面的顶部,您需要设置视图页面的类型。 IE:
在视图的顶部,在标记的第一行中,您将看到类似这样的内容:
将其更改为:
或您尝试传递给视图的任何类型。 “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:
Change that to be:
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.
[编辑] 具体来说,视图是什么样的(例如,它期望获得什么模型,如何解析集合等)
您可以发布一些代码吗? 我正在做类似的事情,但没有任何问题。
[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.
听起来您需要使您的视图成为强类型视图。 让您的视图派生自
ViewPage;
而不仅仅是ViewPage
。 另一个解决方案是将模型转换为视图中的 MembershipUserCollection:It sounds like you need to make your view strongly typed view. Have your view derive from
ViewPage<MembershipUserCollection>
instead of justViewPage
. Another solution is to cast your Model to MembershipUserCollection in your view:尝试将 asp.net 应用程序对象
添加到应用程序对象代码中
,从应用程序对象中检索
这将帮助您
Try asp.net application object
add to application object code
retrive from applicaton object
this will help you