Reputation: 2308
I need help understanding this
In my controller I have
MembershipUserCollection usersObj = Membership.GetAllUsers();
IEnumerator<MembershipUserCollection> model = usersObj.GetEnumerator();
return PartialView(model );
I need an IEnumerator object
to iterate through but I am not sure which type of IEnumerator
MembershipUserCollection.GetEnumerator()
returns.
I want to pass this IEnumerator
to a view and there I´ll use this Enumerator
inside of a foreach
:
@foreach (var membershipUser in Model.getEnumerator())
{ ... }
Upvotes: 0
Views: 443
Reputation: 7336
foreach
iterates throught objects that inherits from the IEnumerable
interface, so if MembershipUserCollection
inherits from it (and it does), you can pass it to the model and it should work.
MembershipUserCollection usersObj = Membership.GetAllUsers();
return PartialView(usersObj);
and in the view
@model System.Web.Security.MembershipUserCollection
<h2>Users</h2>
<ul>
@foreach(var u in Model)
{
<li>u.UserName</li>
}
</ul>
Upvotes: 0
Reputation: 3584
GetEnumerator() Returns an enumerator that iterates through a collection.
Collection must meet the following requirements:
The collection type:
1. Must be one of the types: interface, class, or struct.
2. Must include an instance method named GetEnumerator that returns a type, for example, Enumerator.
The type Enumerator (a class or struct) must contain:
1. A property named Current that returns ItemType or a type that can be converted to it. The property accessor returns the current element of the collection.
for more information see the link below: http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.getenumerator.aspx
Upvotes: 0
Reputation: 43046
Your question assumes incorrectly that this code works:
@foreach (var membershipUser in Model.getEnumerator()) { ... }
In fact, you should write this:
@foreach (var membershipUser in Model) { ... }
With that, your entire question becomes moot. The compiler handles the details of calling GetEnumerator
, and, as you've used the var
keyword, it infers the type of the loop variable, too.
Upvotes: 4