ok I have added the namespace, but now when I run the program, I'm getting a runtime error: NullReferenceException was unhandled by user code on the line: foreach (MyUser u in Model.AllUsers)
Either the view ViewModel Mode is null or Model.AllRoles is null debug your action code, to verify what is written in the model that you pass to the View with:
return View(model)
Put a breakpoint before return View(model) and see what is in model
See if also roles is null. If not there is a type mismatch between allRoles and roles, I suspect the first one is just an IEnumerable, while the secondone is an IEnumerable<MembershipUser>.
Create a List<MyUser>, sat MyList and in a foreach loop on roles add all MyUSer contained in roles to MyList, wuth something like MyList.Add(currItem) then put this list as value of AllUsers.
OK I added public List<MyUser> myList { get; set; } to the ViewModel.
I have set up the controller as
public ActionResult GetUsers()
{
var roles = from MembershipUser u in Membership.GetAllUsers()
select new MyUser
{
User = u,
UserRoles = Roles.GetRolesForUser(u.UserName)
};
MyViewModel model = new MyViewModel { AllUsers = roles };
return View(model);
}
Now this is the part I'm not sure about. I don't know what you mean as currItem. Here's the nested forloop
I suggest you to study the basics of C#, collections and generics, and then to follows some basic tutorial about mvs.
Anyway....I meant something like this:
<div class="im">{
var roles = from MembershipUser u in Membership.GetAllUsers()
select new MyUser
{
User = u,
UserRoles = Roles.GetRolesForUser(u.UserName)
};
List<MyUser> res = new List<MyUser>();</div> <div class="im">foreach(MyUser u in roles) res.Add(u);</div> <div class="im"> </div> <div class="im">MyViewModel model = new MyViewModel { AllUsers = res };
</div>
return View(model);
}
The remainder is ok no need to change anything. PLEASE DEBUG THE CIDE TO SEE WHAT HAPPENS
francesco ab...
All-Star
20912 Points
3279 Posts
Re: showing user roles for users in database
Dec 04, 2011 09:16 PM|LINK
It is likely that MyUser is defined in a namespace that is not included in the View. See what the name of the Namespace is and add
@using namespaceName
Mvc Controls Toolkit | Data Moving Plug-in Videos
multiv123
Member
84 Points
47 Posts
Re: showing user roles for users in database
Dec 04, 2011 09:26 PM|LINK
ok I have added the namespace, but now when I run the program, I'm getting a runtime error: NullReferenceException was unhandled by user code on the line: foreach (MyUser u in Model.AllUsers)
francesco ab...
All-Star
20912 Points
3279 Posts
Re: showing user roles for users in database
Dec 04, 2011 10:21 PM|LINK
Either the view ViewModel Mode is null or Model.AllRoles is null debug your action code, to verify what is written in the model that you pass to the View with:
return View(model)
Put a breakpoint before return View(model) and see what is in model
Mvc Controls Toolkit | Data Moving Plug-in Videos
multiv123
Member
84 Points
47 Posts
Re: showing user roles for users in database
Dec 04, 2011 10:33 PM|LINK
AllUsers is null.
francesco ab...
All-Star
20912 Points
3279 Posts
Re: showing user roles for users in database
Dec 05, 2011 09:25 AM|LINK
See if also roles is null. If not there is a type mismatch between allRoles and roles, I suspect the first one is just an IEnumerable, while the secondone is an IEnumerable<MembershipUser>.
Mvc Controls Toolkit | Data Moving Plug-in Videos
multiv123
Member
84 Points
47 Posts
Re: showing user roles for users in database
Dec 05, 2011 02:26 PM|LINK
I think roles is not null. What should I do to fix it?
multiv123
Member
84 Points
47 Posts
Re: showing user roles for users in database
Dec 05, 2011 05:29 PM|LINK
Please help. This is urgent.
francesco ab...
All-Star
20912 Points
3279 Posts
Re: showing user roles for users in database
Dec 05, 2011 08:18 PM|LINK
Create a List<MyUser>, sat MyList and in a foreach loop on roles add all MyUSer contained in roles to MyList, wuth something like MyList.Add(currItem) then put this list as value of AllUsers.
Mvc Controls Toolkit | Data Moving Plug-in Videos
multiv123
Member
84 Points
47 Posts
Re: showing user roles for users in database
Dec 05, 2011 08:32 PM|LINK
OK I added public List<MyUser> myList { get; set; } to the ViewModel.
I have set up the controller as
public ActionResult GetUsers()
{
var roles = from MembershipUser u in Membership.GetAllUsers()
select new MyUser
{
User = u,
UserRoles = Roles.GetRolesForUser(u.UserName)
};
MyViewModel model = new MyViewModel { AllUsers = roles };
return View(model);
}
Now this is the part I'm not sure about. I don't know what you mean as currItem. Here's the nested forloop
foreach(MyUser u in Model.AllUsers)
foreach (string role in u.UserRoles) {
AllUsers = MyList.Add(currItem);
Console.Write(role); //is this correct?
}
francesco ab...
All-Star
20912 Points
3279 Posts
Re: showing user roles for users in database
Dec 05, 2011 08:40 PM|LINK
I suggest you to study the basics of C#, collections and generics, and then to follows some basic tutorial about mvs.
Anyway....I meant something like this:
<div class="im">{var roles = from MembershipUser u in Membership.GetAllUsers()
select new MyUser
{
User = u,
UserRoles = Roles.GetRolesForUser(u.
};
List<MyUser> res = new List<MyUser>();</div> <div class="im">foreach(MyUser u in roles) res.Add(u);</div> <div class="im"> </div> <div class="im">MyViewModel model = new MyViewModel { AllUsers = res };
</div>
return View(model);
}
The remainder is ok no need to change anything. PLEASE DEBUG THE CIDE TO SEE WHAT HAPPENS
Mvc Controls Toolkit | Data Moving Plug-in Videos